我试图从上到下制作视图幻灯片.这不是什么大问题,我用CABasicAnimation这个.问题是当我想删除视图时.我用这个动画.
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"];
Run Code Online (Sandbox Code Playgroud)
这完美地激发了视图的动画效果.但是,在动画结束后,我的视图再次出现.所以我添加了这一行:
[self.view removeFromSuperview];
Run Code Online (Sandbox Code Playgroud)
这会移除视图,但没有动画.所以我决定将删除代码添加到此委托:
-(void) animationDidStop:(CAAnimation *) animation finished:(bool) flag
Run Code Online (Sandbox Code Playgroud)
所以现在,动画工作,视图消失,但有时,我可以看到视图出现并消失得更快,就像在动画之后,视图出现,然后animationDidStop调用委托,视图消失,显然这很糟糕.我究竟做错了什么?