Replaying a Core Animation animation? - iphone

I was wondering, what if I want to replay an animation I have already added to the layer? Do I need to add the animation to the layer each and every time I want it to play, or there's a way to replay an animation I have already added?
Thanks,
iLyrical.

Once animation gets finished then to repeat animation u will have to add new animation like this:
[yourView.layer removeAllAnimations];
[yourView.layer addAnimation:yourAnimation forKey:#"Key here"];

You can configure the animation to repeat a certain number of times before it finishes using the repeatCount property but if you want to repeat an animation that has already finished you have to add it to the layer again.

Related

How can I synchronize both movement and an animation using cocos2d?

I need help getting a CCSprite to animate and move in a synchronized fashion repeatedly. For example I need to move the sprite from one game board space to the next while walking. While I can call each action right after one another, I have found that when run in rapid succession the two don't fire at the same time causing either an additional animation cycle to run between moves or movements to not be aligned with the start/end of the animation.
Here are examples of how I'm creating the animation and movement actions.
CCAnimation *animRunUR_ = [CCAnimation animationWithFrame:#"actor_run_ur" frameCount:8 delay:runAnimDuration_];
CCAction *_actionAnimRunUR = [[CCSequence actions:
[CCAnimate actionWithAnimation:animRunUR_],
[CCCallFunc actionWithTarget:self selector:#selector(animationFinished)],
nil] retain];
CCAction *_action = [[CCSequence actions:
[CCMoveTo actionWithDuration:duration_ position:gridbox_.center],
[CCCallFunc actionWithTarget:self selector:#selector(nextMovement)],
nil] retain];
Each of the above actions are called directly on the CCSprite as follows.
[self.sprite runAction:_action];
[self.sprite runAction:_actionAnimRunUR];
The duration of the animation is calculated to be equal to the amount of time it takes to move to the next game board space. In addition, these are fairly straight forward actions but I'm really having a hard time keeping them synced as they continually repeat.
Any best practices or suggestions would be greatly appreciated as I'm sure this is a common issue in game development with sprites that continually animate while moving. Then trying to keep smooth transitions between things like walk, run, and jump without split second pauses between actions. Thanks for your feedback and suggestions.

How to state commands of 'animation stop animating'?

I have an app I'm creating and I need some help. Under the view did load I have a code to play music which is fine. Then there is button saying 'play' when they click play the music STOPS and a animation starts. Now after the animation finished 2 buttons appear. How would I state animation stop animating like a void or ibaction so then I can put when the animation stop animating happens play the music again. If this dosent make sense please feel free to ask questions, Im a beginner so take it easy on me :) thanks
+ (void)animateWithDuration:(NSTimeInterval)duration
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion
Would allow you to know when an animation is completed, code in the completion block will be executed when the animation finishes.

How to obtain changes to view position when animating frame?

I need to update a view continuously depending on another view's position while it's moving across the screen. I tried using KVO on frame, but it seems to trigger only at the beginning of the animation. Is there a recommended way of doing this?
You want to have a look at the Core Animation presentation layer which should tell you the position during the animation.
I would add a repeatable timer and make something like:
[self updateView:_view
usingPosition:[_anotherView.presentationLayer position]];
(Don't you know _anotherView's animation in advance? Maybe then you can setup _view animation directly?)
Why not just set both animations for both view at the same time?
[UIImageView animateWithDuration:aDuration
delay:0
options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState
animations:^(void) {
view1.center = somePosition;
view2.center = someOtherPosition;
} completion:NULL];
They will move at the same time and with the option : UIViewAnimationOptionBeginFromCurrentState if you start an other animation before this one is finished the views will start at the point they were stopped.
But if it's not what you are looking for, can you update your question with precision on how view1 is moving.

Animate Train in iphone

I want to animate a 20 wagon train in iphone. The train will move left to right. Each wagon will have a different animation. I was confused how to do it. Because the images are large and 20 images at one time would cause memory warnings.
Can anyone suggest how to go about it. Should I use cocos2d for this?
Thanks!
I would do that in cocos2d.
I would create a 'Wagon'-class and then start by initiate two of them (just because I think that two wagons will fill the screen). And then start moving the wagons. Each time a wagon is completely out of the screen I would release it. When it's time for the next wagon I would initiate that and so on.
How many frames does the animations have, and how big are they? Maybe you'll have to make one atlas per wagon. You can always call:
[[CCTextureCache sharedTextureCache] removeUnusedTextures];
and:
[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
Cocos2D is best!
Create sprites, set actions to it and IMHO!!!
If you want to do using UIImageViews, look for
UIImageView.animationImages
UIImageView.animationDuration
UIImageView.animationRepeatCount
and
[UIView beginAnimations:(NSString *)animationID context:(void *)context];
[UIView setAnimationDuration:(NSTimeInterval)duration]
// make the changes in your view which you want to occur in given duration
[UIView commitAnimations];
But again, this will definitely give you a hard time, better go for Cocos2D.

What is the best way to play an animation frame by frame?

I draw some GIS data dynamically, based on user's control, into CGImageRef.
But how to play these frame like an animation efficiently in an UIImageView?
After searching and surveying, the way to play an animation in UIImageView is preloading all images you need,and invoking the startanimating method,like this:
myImageView.animationimages=[NSArray arrayWithObjects:1uiimage,2uiimage,....,nil];
[myImageView startAnimating];
But the waiting time is too long if i preload all UIImages, because i have more than 400 frames each time.
I want to play the animation like a stream,real time stream, how to do that?
Deeply appreciating if you could give me any idea or an example. :)
Use CADisplayLink to get notified on each screen refresh. Change myImageView.image in the callback.
(You might have to do something more clever if you want to limit the framerate.)