28

I am trying to make a view slide from top to bottom. This is not a big deal, I used CABasicAnimation for this. The problem is when I want to remove the view. I use this animation.

CABasicAnimation *animation;
animation = [CABasicAnimation animationWithKeyPath:@"position"];
[animation setDelegate:self];
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(self.view.layer.position.x, 0 - self.view.bounds.size.height / 2)];
animation.fromValue = [NSValue valueWithCGPoint:self.view.layer.position];
animation.autoreverses = NO;
animation.repeatCount = 0;
animation.duration = 0.25;
animation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut];
[self.view.layer  addAnimation:animation forKey:@"moveX"];

Which animates the view perfectly. But, after the animation finishes, my view appears again. So I added this line :

[self.view removeFromSuperview];

Which removes the view, but with no animation. So I decided to add the remove code to this delegate:

-(void) animationDidStop:(CAAnimation *) animation finished:(bool) flag

So now, the animation works, the view disappears, but sometimes, I can see the view appear and disappear faster, is like after the animation, the view appears, then the animationDidStop delegate is called, and the view disappears, obviously this is awful. What am I doing wrong?

Robert
  • 1,286
  • 1
  • 17
  • 37
carlos
  • 2,624
  • 4
  • 26
  • 36

6 Answers6

66

Might want to set these properties. They cause the presentation to be preserved at the end of the animation.

animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;

Then the "animationDidStop:" method can be used to remove the view at the end of the animation:

-(void) animationDidStop:(CAAnimation *) animation finished:(bool) flag {
    if (animation == [containerView.layer animationForKey:@"moveX"]) {
        // remove view here, add another view and/or start another transition
    }
}
FreeAsInBeer
  • 12,937
  • 5
  • 50
  • 82
  • +1! I tried to set `removedOnCompletion` but no dice. Adding `fillMode = kCAFillModeForwards` in addition fixed it, though. Great! – August Lilleaas Sep 26 '10 at 18:29
  • 1
    When CA starts animating a layer, it makes a copy and then incrementally sets properties on it and redraws it. This technique does work, but it keeps animating the presentation layer forever (with the same value again and again). If you want to let the presentation layer die, you may want to link the setter in the presentation to the one in the model layer. One way to do this is to use an object pointer for your property, like an NSValue, rather than a primitive. Then when the presentation layer sets the property, the model layer will pick it up too. – AlexChaffee Apr 06 '13 at 16:17
  • Note, I tried this method for my animation of the background colour, but it sometimes produced a flicker back to the original colour. Surprisingly, using [Ben's solution](http://stackoverflow.com/a/227798/377384) worked (no flicker so far). – lindon fox Jan 03 '15 at 09:39
  • This method is useful when the animation is interactive, u can get the position of the present layer and make further animation. But when it is not interactive, @Ben 's method is simpler. – Nickolas Apr 02 '15 at 03:41
  • This issue should have the green check! – ReduxDJ Aug 11 '15 at 19:06
15

Well, according to the Apple sample "MoveMe", this (removedOnCompletion) should work, however, it doesn't seem to.

So, add these lines after your code:

[self.view.layer  addAnimation:animation forKey:@"moveX"];
self.view.layer.position = [animation.toValue CGPointValue];

This ensures that after the animation runs, the layer is properly positioned.

tipycalFlow
  • 7,594
  • 4
  • 34
  • 45
Ben Gottlieb
  • 85,404
  • 22
  • 176
  • 172
  • Why didn't I see that? Thanks, This solved my problem. I think this is an error, I shouldn't be doing that, but at least it works. – carlos Oct 23 '08 at 00:45
  • I agree, or we're missing something, since it DOES work in the sample. Need to experiment more, I guess! – Ben Gottlieb Oct 23 '08 at 01:23
3

I had this issue when performing several animations in an animation group. I had to set a couple properties on the animation group itself, not the individual animations.

CAAnimationGroup *animGroup = [CAAnimationGroup animation];

// MAKE SURE YOU HAVE THESE TWO LINES.
animGroup.removedOnCompletion = NO;
animGroup.fillMode = kCAFillModeForwards;

animGroup.animations = [NSArray arrayWithObjects:moveAnim, scaleAnim, nil];
animGroup.duration = tAnimationDuration;
[tImageView.layer addAnimation:animGroup forKey:nil];
FreeAsInBeer
  • 12,937
  • 5
  • 50
  • 82
0

Setting the view to hidden as Rob suggests should do it.

For properties of properties I would stick with the ObjC 2.0 style like you already have in your code.

set.view.hidden = YES;
DD_
  • 7,230
  • 11
  • 38
  • 59
danimal
  • 516
  • 1
  • 8
  • 21
0

This one bit me too. You want to set the animation's removedOnCompletion flag to NO. It defaults to YES, which means after the animation is complete, it's removed, and the view reverts to its initial state.

Ben Gottlieb
  • 85,404
  • 22
  • 176
  • 172
  • Same here, I tried this and same result. I am calling this code from the same view, when they press a button. – carlos Oct 22 '08 at 20:25
-2

Can you set the view's hidden property to YES?

I think it would be:

self.view.hidden = YES;

But it might be:

[self.view setHidden:YES];

I turns out I am pretty lame at figuring out the proper way to access properties of properties.

DD_
  • 7,230
  • 11
  • 38
  • 59
Rob Drimmie
  • 1,586
  • 1
  • 13
  • 16