这是一个有点棘手的搜索。不太确定要敲击谷歌或 SO,所以如果之前已经回答过这个问题,我深表歉意。
所以我有两个动画,我应用到CALayer
一个持续时间为 5 秒的动画(尽管这不相关)并且它们无限期地重复。我希望能够在用户交互时优雅地删除这些动画。
检测交互很容易,但能够确定动画何时结束一个循环并不容易。通过检测到这一点,我希望实现动画完成最后一个循环并停止的效果,而不是从看起来很不友好的屏幕上粗暴地删除它。
这就是我现在正在做的事情,但它不起作用
- (void)attachFadeAnimation {
// Create a fade animation that compliments the scale such that
// the layer will become totally transparent 1/5 of the way
// through the animation.
CAKeyframeAnimation *fadeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
fadeAnimation.values = @[@0.8, @0, @0];
[self addAnimation:fadeAnimation withKeyPath:@"opacity"];
}
- (void)addAnimation:(CAKeyframeAnimation *)animation withKeyPath:(NSString *)keyPath {
// These are all shared values of the animations and therefore
// make more sense to be added here. Any changes here will
// change each animation.
animation.keyTimes = @[@0, @0.2, @1];
animation.repeatCount = HUGE_VALF;
animation.duration = 5.0f;
animation.delegate = self;
[self.layer addAnimation:animation forKey:keyPath];
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
if ( !self.emanating )
[self.layer removeAllAnimations];
}
animationDidStop:finished
当我期望它时,没有调用委托调用。显然我误解了文档。