这个有点棘手,不太确定该在Google或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
没有在我预期的时候被调用。显然,我误解了文档。