【问题标题】:CAAnimation delegate methods not called未调用 CAAnimation 委托方法
【发布时间】:2015-06-09 04:11:31
【问题描述】:

我在使用 iOS CABasicAnimation 时遇到问题。无论我做什么,我都无法触发 animationDidStart:animationDidStop:finished: 方法。我的类是 CAShapeLayer 的子类,并在其中执行动画:

- (void)start{
    [self removeAllAnimations];

    CABasicAnimation *pathAnimation = [self makeAnimationForKey:@"strokeEnd"];
    [self addAnimation:pathAnimation forKey:@"strokeEnd"];
}

- (CABasicAnimation *)makeAnimationForKey:(NSString *)key {
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:key];
    anim.fromValue = [NSNumber numberWithFloat:0.f];
    anim.toValue = [NSNumber numberWithFloat:1.f];

    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    anim.duration = self.duration;
    anim.delegate = self;
    return anim;
}

- (void)animationDidStart:(CAAnimation *)anim{
    NSLog(@"HERE START");
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    NSLog(@"HERE STOP");
}

任何提示或帮助将不胜感激,在此先感谢!

【问题讨论】:

  • 请记住,图层在动画时会被克隆,因此使用这样的子类可能无法获得所需的效果。
  • 感谢大卫的提醒,我一定会记住的。
  • 请注意CAAnimationdelegate 很强大,因此您可能需要将其设置为nil 以避免保留循环!

标签: ios objective-c caanimation


【解决方案1】:

您必须在将动画分配给图层之前设置委托:

popIn.delegate = self;
[annotation.layer addAnimation:popIn forKey:@"popIn"];

斯威夫特 5.4.2

popIn.delegate = self
annotation.layer.add(transition, forKey:"popIn")

【讨论】:

    【解决方案2】:

    好的,所以在我的子类中,我有一个名为持续时间的属性。尽管它没有被记录为是CALayer 的一部分,但持续时间是它的协议之一,称为CAMediaTiming。这些方法从未被触发,因为该属性正在通过我的子类被覆盖。

    【讨论】:

    • 这算作记录给我。您通常不会提及在对象符合的协议中声明的属性或方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 2016-07-16
    • 2019-01-04
    • 2014-02-07
    相关资源
    最近更新 更多