iOS class that allows an image to "drift" across screen? - ios

I've seen a lot of helpful tutorials that show one how to:
make an image move according to a predefined path, or
move the image, a few pixels at a time, in response to a UIButton.
What I want to do is have the image "drift" arbitrarily according to an Vxy velocity I define, then have the button(s) change the velocity. (Yes, I'd have it slow down with time if no action made).
In other languages there might have been a way to do Change Pxy position by Vxy (to ad infinitum) unless button pushed. I believe GET was the command. I can think of a way to do that in iOS I suppose but that would involve setting up a series of 1 sec CGMutablePathRef anims. Alternatively, I have seen some talk of NSTimer: would it be a good practice to introduce some sort of delay: draw, delay, draw, delay.
Request: specific classes or terms I can search in the manuals for myself.

Iirc using uiview's animateWithDuration:completion is cheaper than using core animation. frame is an animatable property. So, yeah I think I would use an NSTimer to call your method for default calculation of the end frame of your view and then call animateWithDuration:completion there.

[deleted bad idea]
I ran across a wonderful tutorial for anyone considering such a project;
http://www.youtube.com/watch?v=nH_Rj152DRM
I believe the key "noob" problem I was having was in not realizing I should declare the instance variable for my sprite/ image in the
-(void) viewDidLoad{
then work on other properties of the animation in touches/ other user events. Once I figured that out, I am now capable of doing the heavy lifting for the rest of the project myself.

Related

Is it possible to render a tableview 'skewed'?

I'm coding in Swift 2.0 for devices running iOS7+.
Is it possible to present a tableview in a skewed/diagonal/slanted format as indicated below?
Obviously if the answer is yes, what process would I need to go through to get the result?
Yes it's possible. Views in iOS have a transform property, of type CGAffineTransform. You can use that to make the view appear skewed. I don't know offhand how to create a transform that creates the skewing effect. I suggest doing some google searching.
The next issue you will face is interacting with taps. Changing the transform of a view does not transform the coordinate system applied to taps, so taps will still land on the non-skewed views. That will be much harder to sort out, and without doing a fair amount of research I don't have an answer for you on that one. (It would probably be possible to intercept touch events before they get to your table view and apply the inverse of your skewing transform to them so that you map the taps back to the rectangular coordinate system the table view is expecting.)

Making a UISlider move back and forth on its own

In my current project I've created an app that utilizes the position of the UISlider in relation to a target value displayed to the user. I've got full functionality however I'm now trying to spice it up a bit.
Is it possible to set the UISlider to move from (minimum through maximum) than (maximum through minimum) back and forth on its own when the user enters the app rather than having them drag it?
Thoughts:
Possibly use a looping sequence?
Not looking for someone to code this for me I quite enjoy that part. Just looking for guidance.
Further to the NSTimer answer, I wouldn't suggest using that, but CADisplayLink instead. Similar process, but instead of using NSTimer use CADisplayLink.
CADisplayLink synchronises with the screen refresh rate, so you can adjust the animation and slider value based on current frame, so the animation appears smooth.
http://patzearfoss.com/2011/09/02/more-cadisplaylink/
I think that in this case you would have to setup an NSTimer with a time that you wish per frame, separate out the action code within the slider into a routine that can be called by you, then within the timer action code, call your routine, and then set the slider position manually.
Look here for setting up an NSTimer
How do I use NSTimer?
The NSTimer will essentially allow you to setup any framerate you wish, AND keep it off of the MAIN thread.
UISlider has this method that allows you to programatically change the slider's value:
- (void)setValue:(float)value animated:(BOOL)animated
Be careful about using loops though, you don't want to block your main thread an make your app unresponsive.
Trumpetlick's answer provides good information on using an NSTimer, which should help you with this.

Is the most efficient way to constantly update a label on screen to simply change the label's text property?

Say I have to update a label 1000 times a minute. Completely change its string value. Am I best off making 1000 setText: calls per minute? Or is there a more efficient way? Perhaps doing away with labels completely?
You basically have two options - update the textproperty of a UILabel or draw an NSString directly onto a UIView (after first erasing the previous value). You'll have to run some tests to see which is more efficient.
You can update some local field/variable and use Timer which read value from variable as frequent as it necessary and update Label. You can also use some throttle strategies.
I'm not an iOS expert so I say what my intuition says to me.
I've used this github library in one of my projects https://github.com/dataxpress/UICountingLabel, which basically subclasses a UILabel and counts down from some initial value to some final value with a custom duration that you can specify, and it is quite efficient and smooth. You can have a look at their code and it might help.

Design pattern for large quanity of synchronized animations

I have over 150 UIImageViews. I need them to behave like a large ballet. I also need to be able to dynamically choreograph them. Some spin, some duck, some jump (each currently a different UIImageView subclass).
I'm assuming I need to put them all into an NSDictionary, then grab the object based on a key and say "you spin", "you jump", "you duck". And do those moves in sync while the music is playing (no real music, but instead data coming in from an external source). And I have no idea what that music(data) will be until it arrives, and no one's heard the song before. I've setup a while (music){} loop with a large switch statement inside.
I'd like to place all the ballerina's on the view in the nib by hand so that they line up correctly with the subview. None of them change x,y. They might only change z, and maybe swap an image, fade opacity, etc (which I was calling spin, duck and jump). And there is no user interaction here, you're just in the audience.
I'm also assuming I'll need to use the UIView's beginAnimation, setAnimation, commitAnimation methods.
Am I on the right track? What's the best way to achieve this? Any optimizations I should consider?Apologies for all the analogies, it's the easiest way to explain what I'm trying to achieve.
I recommend you look at UICollectionView. There was a great WWDC session about it. I'm not exactly sure what you're trying to achieve, but with UICollectionView you can set up custom layouts and animate between different layouts automatically. So if your images have to move in sync, this might be a good option.

Scrolling items on screen [iOS cocos2d]

OK so in my game I need the users to scroll between items, just like you scroll a web page in Safari. Is there any way to do that? If not, maybe scrolling them to the side, like you do in the spriboard? Thanks.
I am not really sure I have understood what you would like to do, but there is a cocos2d extension that seems appropriate to it: CCScrollLayer.
CCLayer subclass that lets you pass-in an array of layers and it will then create a smooth scroller. Complete with the “snapping” effect.
If you are looking for a generic scrolling within your view, I suggest this tutorial or this topic rom cocos2d list.
EDIT:
I have never done it, but I think it should be possible to scale the CCScrollLayer to the size you need.
Otherwise, you might change the contentSize of the layer, or even put the CCScrollLayer into a clipping node.
Anyway, I think that it is much easier to start from this and find a way to adapt it to your specific requirements than start from scratch.

Resources