6

It's easy enough to animate the view:

[UIView animateWithDuration:1.0
                     animations:^{theView.center = newCenter; theView.alpha = 0;}
                     completion:^(BOOL finished){
                         [theView removeFromSuperview];
                     }];

The problem is that when I add it as a subview, I want it to fade in and already look like it is moving. Right now it appears immediately, then moves and fades out.

So, I need to set it's initial alpha to zero, fade it quickly while it is moving, then fade it out. Is this possible with UIView animations? I can't have two competing animation blocks working on the same object right?

soleil
  • 12,133
  • 33
  • 112
  • 183
  • I'm not following you: Your code is typical "slide and fade out" example. But then you talk about "when I add it as a subview". Are you adding it? Or removing it? But, in answer to your question, you should not need multiple animation blocks. It's just a question of what you're trying to do. – Rob Feb 08 '13 at 19:14
  • Here's a similar post with a lot of good animation answers: http://stackoverflow.com/questions/5040494/objective-c-ios-development-animations-autoreverse – escrafford Feb 08 '13 at 19:16

2 Answers2

13

All you need to do is apply 2 animations back-to-back. Something like this ::

theView.alpha = 0;
[UIView animateWithDuration:1.0
                 animations:^{
                     theView.center = midCenter;
                     theView.alpha = 1;
                 }
                 completion:^(BOOL finished){
                     [UIView animateWithDuration:1.0
                                      animations:^{
                                          theView.center = endCenter;
                                          theView.alpha = 0;
                                      }
                                      completion:^(BOOL finished){
                                          [theView removeFromSuperview];
                                      }];
                 }];

So in the 1st second it will appear while moving and then in the next second it will fade out

Hope this helps

Rob
  • 415,655
  • 72
  • 787
  • 1,044
Dhruv Goel
  • 2,715
  • 1
  • 17
  • 17
  • This should work, even though it requires an extra center point to calculate. I ended up using a CABasicAnimation for the movement and a UIView animation for the alpha. – soleil Feb 08 '13 at 23:23
2

Put the initial alpha=0 outside animation block.

theView.alpha = 0;
[UIView animateWithDuration:1.0
                 animations:^{
                     theView.center = newCenter; 
                     theView.alpha = 1;
                 }
                 completion:^(BOOL finished){
                     // Do other things
                 }];
Ben Lu
  • 2,982
  • 4
  • 31
  • 54