iPhone SDK - how can I tell when an animation has finished? - iphone

I am starting an animated enlargement when an image is touched, and then scaling it back down to normal size when it is released. By using setAnimationBeginsFromCurrentState:YES the zooming effect is nice and smooth if you lift your finger part way through animating.
However, what I want to do is "lock" the larger size in place if you've touched the image for long enough for the animation to complete, but let it shrink back down as normal if you release prematurely.
Is there a way to tell whether there is currently an animation running, or whether a particular animation has completed?
I figure I can probably do this with a performSelector:afterDelay: call in touchesStarted, with a delay equal to the length of the animation and cancelling it if touchesEnded comes too soon, but I can't imagine that's the best way...?

- (void)animateStuff {
[UIView beginAnimations:#"animationName" context:nil];
[UIView setAnimationDelegate:self];
[self.view doWhatever];
[UIView commitAnimations];
}
- (void)animationDidStop:(NSString *)animationID
finished:(NSNumber *)finished
context:(void *)context
{
if ([finished boolValue]) {
NSLog(#"Animation Done!");
}
}

Another possibility:
[UIView animateWithDuration:0.3 animations:^{
myView.transform = CGAffineTransformMakeRotation(M_PI);
}completion:^(BOOL finished) {
NSLog(#"Animation complete!");
}];

I think "+ (void)setAnimationDidStopSelector:(SEL)selector" should do what you want. It will call the given selector on your delegate once the animation has completed.

Related

How to check that UIImageView has completed its animation

I am new to iPhone development. I am using UIImageView to animate the images in an array. I am deciding that the animation is stopped through a property called isAnimating. But it always returns true.
I want to check that the animation is completed or there is some duration left to complete the animation.
Please let me know, how I can check it.
Make a property BOOL IsAnimating
then do this code
{
[UIView beginAnimations:#"Animation of ImageVIew Begins" context:nil];
IsAnimating =YES;
[UIView setAnimationDuration:0.4]; //you can put your time
NSLog(#" Animating");
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(AnimationComplete)];
// Your animation code here
[UIView commitAnimations];
}
- (void) AnimationComplete
{
isAnimating = NO;
}
Your BOOl variable will be YES till the animation is complete..then it will change to NO
Use a delegate based on the duration to trigger whatever method you want on the receiver.
you should be doing your animation inside animation blocks:
[UIView animateWithDuration:duration animations:^{
//animation code here
} completion:^(BOOL finished)
{
//this will be called when the animation is finished
}];
edit: this answer is wrong and probably shouldnt have been accepted, for anyone coming here looking for the right answer: this answers this question i think

Flip animation is ignoring duration, i.e. I can't control speed

So I attempt to flip between two images, but the duration is seemingly ignored.
I understand that Apple has to do some animation very quickly to keep the magic alive, but is there a way to slow down the flipping?
My current object hierarchy is game board contains many cards which inherit from UIView.
A card has a face and back that are UIViews that host a UIImage and other data
[UIView animateWithDuration:15.0
animations:^(void) {
cardOne.center =CGPointMake(100, 100);
cardTwo.center =CGPointMake(100, 100);
//low level calls, to directly perform the image swap
[cardOne.face removeFromSuperview];
[cardOne addSubview:cardOne.back];
[cardOne sendSubviewToBack:cardOne.face];
//calling the transitionFromView to do the image swap
[UIView transitionFromView:cardOne.face
toView:cardOne.back
duration:10
options:UIViewAnimationTransitionFlipFromLeft
completion:^(BOOL finished){ }];
Thanks for your feedback. I attempted a mix of all you input and it did end up working correctly. I don't yet know why though.
Thanks for your responses. I tried reverting to the old style and it worked. I have not figured out why as yet.
The non-working code is commented out and the working code is left uncommented.
Thanks again
/*
[UIView animateWithDuration:animationDuration
animations:^{
[self.face removeFromSuperview];
[self addSubview:self.back];
[self sendSubviewToBack:self.face];
}];
*/
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kDefaultFlipSpeed];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self
cache:YES];
[self.face removeFromSuperview];
[self addSubview:self.back];
[self sendSubviewToBack: self.face];
[UIView commitAnimations];
I cant ask in a comment, so:
Have you tried with less than 10 seconds? More precisely between 0-1 second. Something like 0.5 seconds. Tell me if you notice any change in duration with these values.
My quick answer would be:
Try setting 10 seconds as 10.0 or 10.0f.
Hope it helps.

Moving an object without animating it in an animation block

I'm animating a bunch of buttons so they move to the left of the screen then they should magically move to the right of the screen. I do this by calling:
[NSTimer scheduledTimerWithTimeInterval:0.20 target:self selector:#selector(moveTheButtons) userInfo:nil repeats:YES];
in viewDidLoad.
They animate quite happily to the left, but then they animate back to the right. I just want them to disappear and reappear to the right-off-screen. Because I'm new I assumed that since it was after the commitAnimations call that it wouldn't animate. I think the problem is that the animations actually 'commit' after the moveTheButtons function returns, but I can't think of an elegant (or standard) way around this.
How do I move the UIButton off screen without animating it, preferably still in the moveTheButtons function?
As you can probably infer, I'm new to animations on the iPhone, so if you see any other mistakes I'm making feel free to give me some pointers.
-(void)moveTheButtons{
NSLog(#"moveTheButtons");
[UIView beginAnimations:#"mov-ey" context:cloudA];
[UIView setAnimationDuration: .20];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
cloudA.center = CGPointMake(cloudA.center.x-pos.x,cloudA.center.y);
[UIView commitAnimations];
if(cloudA.center.x < -100){
//I don't want to animate this bit.
cloudA.center = CGPointMake(550,cloudA.center.y);
}
//NSLog(#"cloudA.center.x %f", cloudA.center.x);
}
You can temporarily turn off the implicit animations of properties within an animation block using the +[UIView setAnimationsEnabled:] method. This can be very useful in many use cases.
Do something like this:
-(void)moveTheButtons {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: .20];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
CGPoint center = CGPointMake(cloudA.center.x - pos.x, cloudA.center.y);
if (center.x < -100) {
[UIView setAnimationsEnabled:NO];
cloudA.center = CGPointMake(550, center.y);
[UIView setAnimationsEnabled:YES];
} else {
cloudA.center = center;
}
[UIView commitAnimations];
}
As a side note; there is no need to give the animation a name, or a context, unless you actually use a delegate that responds to delegate methods. Just pass nil, and NULL as I have done in this example.
Precalculate the point , invert the order and return before animating the block.
You are unconditionally committing an animation to the engine in all cases.
-(void)moveTheButtons{
NSLog(#"moveTheButtons");
CGPoint mypoint = CGPointMake(cloudA.center.x-pos.x,cloudA.center.y);
if(cloudA.center.x < -100){
//I don't want to animate this bit.
cloudA.center = CGPointMake(550,cloudA.center.y);
return;
}
[UIView beginAnimations:#"mov-ey" context:cloudA];
[UIView setAnimationDuration: .20];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
cloudA.center = mypoint;
[UIView commitAnimations];
}
Theres a style point about using a return rather than if/else but you can form your own opinion.
Cheers

How to know when a Core Animation has finished?

say I have...
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
CGPoint position = myObject.center;
position.x = position.x - 10;
myObject.center = position;
[UIView commitAnimations];
Core animation happens on a separate thread is there a way to know when
an animation has finished? i.e., maybe there's some way I can hook up a
function call to know when it got finished... ?
(p.s I know I can use a timer that fires a method after say 0.5s in this
above example, but that seems pretty cheesy)
any help much appreciated!
You can set the setAnimationDidStopSelector:
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/clm/UIView/setAnimationDidStopSelector:
(void)setAnimationDidStopSelector:(SEL)selector
Then implement a method for that selector
[UIView setAnimationDidStopSelector:#selector(finishedAnimation:finished:context:)];
- (void) finishedAnimation:(NSString *)id finished:(BOOL) finished context:(void *) context {
......
}
Hope that helps.

iphone setAnimationWillStartSelector/setAnimationDidStopSelector not working (a scrolling ticker example)

I am displaying a ticker at the bottom of a view (think of a news channel's headlines ticker bar) in the form of a horizontal scrollview. It works correctly when I set the repeatCount to infinite but I want to be able to do some other functionality when the animation starts and stops instead. However, after reading the documentation and many examples online, I can't get setAnimationWillStartSelector/setAnimationDidStopSelector to respond.
Here's my code:
- (void)animateView {
[UIScrollView setAnimationDelegate:self];
[UIScrollView setAnimationWillStartSelector:#selector(animationStart:context:)];
[UIScrollView setAnimationDidStopSelector:#selector(animationStop:finished:context:)];
[UIScrollView beginAnimations:#"pan" context:nil];
[UIScrollView setAnimationDuration:10.0f];
[UIView setAnimationRepeatCount:1];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIScrollView setAnimationCurve:UIViewAnimationCurveLinear];
tickerScrollView.contentOffset = CGPointMake(textLabelRect.size.width,0);
[UIScrollView commitAnimations];
}
- (void)animationStart:(NSString *)animationID context:(void *)context {
NSLog(#"animationWillStart");
}
- (void)animationStop:(NSString *)animationID finished:(BOOL)finished context:(void *)context {
NSLog(#"animationDidStop");
[self animateView];
}
At the moment this code is in my UIViewController subclass. However, I have also tried putting it all in my app delegate as well, whilst also changing the setAnimationDelegate obviously. I've tried using various animationDurations, repeatCounts, etc but still no luck.
Would really appreciate any help. Thanks
You can try putting the setAnimationDelegate, setAnimationWillStartSelector and setAnimationDidStopSelector inside the animation block. According to the iPhone OS Reference Library documentation, these methods have to be put inside the animation block in order for it to work.
Hope this helps!
aobs