我试图从上到下制作视图幻灯片.这不是什么大问题,我用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"];
这完美地激发了视图的动画效果.但是,在动画结束后,我的视图再次出现.所以我添加了这一行:
[self.view removeFromSuperview];
这会移除视图,但没有动画.所以我决定将删除代码添加到此委托:
-(void) animationDidStop:(CAAnimation *) animation finished:(bool) flag
所以现在,动画工作,视图消失,但有时,我可以看到视图出现并消失得更快,就像在动画之后,视图出现,然后animationDidStop
调用委托,视图消失,显然这很糟糕.我究竟做错了什么?
可能想要设置这些属性.它们使演示文稿在动画结束时保留.
animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;
然后可以使用"animationDidStop:"方法删除动画结尾处的视图:
-(void) animationDidStop:(CAAnimation *) animation finished:(bool) flag { if (animation == [containerView.layer animationForKey:@"moveX"]) { // remove view here, add another view and/or start another transition } }
好吧,根据Apple样本"MoveMe",这个(removedOnCompletion)应该可行,但是,它似乎没有.
所以,在代码后添加以下行:
[self.view.layer addAnimation:animation forKey:@"moveX"]; self.view.layer.position = [animation.toValue CGPointValue];
这可确保在动画运行后,图层已正确定位.