After Animation, View position resets

2019-01-17 03:41发布

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?

6条回答
做自己的国王
2楼-- · 2019-01-17 03:49

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.

查看更多
来,给爷笑一个
3楼-- · 2019-01-17 03:50

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.

查看更多
forever°为你锁心
4楼-- · 2019-01-17 03:53

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];
查看更多
别忘想泡老子
5楼-- · 2019-01-17 03:54

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
    }
}
查看更多
一夜七次
6楼-- · 2019-01-17 03:54

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.

查看更多
何必那么认真
7楼-- · 2019-01-17 04:07

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;
查看更多
登录 后发表回答