After Animation, View position resets

2019-01-17 03:35发布

问题:

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?

回答1:

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.



回答2:

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
    }
}


回答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];


回答4:

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.



回答5:

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;


回答6:

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.