sudo rm -rf
sudo rm -rf

Reputation: 29524

UIView animations not working right

I have an animation that "lifts" up a button and drops a shadow. Here's how I do it, for example.

button1shadow.alpha = 0;
button1shadow.hidden = NO;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];
CGRect frame11 =searchByName.frame;
frame11.origin.y -=20;
frame11.origin.x +=20;
searchByName.frame = frame11;
searchByNameLabel.frame = frame11;
CGRect frame21 = button1shadow.frame;
frame21.origin.y +=10;
frame21.origin.x -=10;
button1shadow.frame = frame21;
searchByName.alpha      = 1;
button1shadow.alpha     = 0.1;
[UIView commitAnimations]; 

This works just fine if I run it by itself.

Then I put this code right after the code above.

button1shadow.hidden = NO;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:2.0];
[UIView setAnimationDuration:1.5];
CGRect frame11 =searchByName.frame;
frame11.origin.y +=20;
frame11.origin.x -=20;
searchByName.frame = frame11;
searchByNameLabel.frame = frame11;
CGRect frame21 = button1shadow.frame;
frame21.origin.y -=10;
frame21.origin.x +=10;
button1shadow.frame = frame21;
searchByName.alpha      = 1;
button1shadow.alpha     = 0.1;
[UIView commitAnimations]; 

Both work fine seperately, but when I combine them right after each other the first animation is not displayed, but the button jumps up to the coordinates I assigned instead of animating. Any ideas?

Edit: I also tried to call the second animation by using:

[NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(lowerButtons) userInfo:nil repeats:NO];

However, it didn't seem to even call "lowerButtons".

Upvotes: 1

Views: 1078

Answers (3)

Nick Forge
Nick Forge

Reputation: 21464

When you commit an animation, the properties on the UIView (or CALayer) are set straight away. E.g. if you move a view up by 100px, as soon as you've committed that animation, the view's frame will show that it is 100px, even though it doesn't look like it (yet). If you want to queue animations up one after the other, you need to look at using CAAnimations directly, or setting up the second animation in the animationDidStop: delegate callback from the first one. To register for the delegate callback, see +[UIView setAnimationDelegate:].

Alternatively, if the second animation is simply the first one in reverse, you can try using the +[UIView setAnimationRepeatAutoreverses:] and +[UIView setAnimationRepeatCount:] methods.

Upvotes: 1

Jeff Kelley
Jeff Kelley

Reputation: 19071

You’re setting an animation delay, so this should work, but have you tried doing the second animation in a delegate callback? Check out UIView’s +setAnimationDelegate: and +setAnimationDidStopSelector: methods.

Upvotes: 2

Brad
Brad

Reputation: 11515

I think the way it works is that if you started one animation with a given AnimationID, then ran another animation with the same ID, the second would "override" the first.

Thus, the answer would be to use two different animationIDs in each beginAnimation block.

I'm not quite sure how the fact that you are passing "nil" affects this logic. Only one way to find out...

Upvotes: 0

Related Questions