views:

2016

answers:

5

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 dissapears, 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 dissapears, obviously this is awful. What am I doing wrong?

Thanks.

A: 

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.

Rob Drimmie
I did this but I got the same results.
Carlos Hernandez
A: 

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;

danimal
A: 

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
Same here, I tried this and same result. I am calling this code from the same view, when they press a button.
Carlos Hernandez
+5  A: 

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

However, by adding this line 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.

Ben Gottlieb
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 Hernandez
I agree, or we're missing something, since it DOES work in the sample. Need to experiment more, I guess!
Ben Gottlieb
+10  A: 

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;

+1! I tried to set `removedOnCompletion` but no dice. Adding `fillMode = kCAFillModeForwards` in addition fixed it, though. Great!
August Lilleaas