核心动画-animationDidStop未调用

2024-02-15

there.

在iOS应用程序中,核心动画回调不起作用。

- (void)startAnim {
    CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    anim.fromValue = startAngle;
    anim.toValue = endAngle;
    anim.duration = 2;
    anim.delegate = self;

    [self.target addAnimation:anim forKey:nil];   // self.target is CALayer instance, it's sublayer of Custom UIView
}

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {
   [self.target setValue:@(endAngle) forKeyPath:@"transform.rotation.z"];
}

但animationDidStop永远不会被调用。 如果我像下面这样更改代码,则会调用完成阻止。

- (void)startAnim {
    CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    anim.fromValue = startAngle;
    anim.toValue = endAngle;
    anim.duration = 2;
    anim.delegate = self;

    [CATransaction begin];
    [CATransaction setCompletionBlock:^{
        [self.target setValue:@(endAngle) forKeyPath:@"transform.rotation.z"];
    }];

    [self.target addAnimation:anim forKey:nil];
    [CATransaction commit];
}

但我不想使用 CATransaction。 为什么不调用animationDidStop?

Update:有一种方法可以设置最终值,例如

- (void)startAnim {
    CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    anim.fromValue = startAngle;
    anim.toValue = endAngle;
    anim.duration = 2;
    anim.delegate = self;

    [self.target setValue:@(endAngle) forKeyPath:@"transform.rotation.z"];
    [self.target addAnimation:anim forKey:nil];
}

但最终的分配应该在动画完成时完成。因为layer有多个动态动画,所以我不知道最终的值。


我找到了animationDidStop没有被调用的原因。

因为动画是在其他线程的循环中添加的,

所以我修复如下。

- (void)startAnim {
    dispatch_async(dispatch_get_main_queue(), ^{
        CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        anim.fromValue = startAngle;
        anim.toValue = endAngle;
        anim.duration = 2;
        anim.delegate = self;

        [self.target addAnimation:anim forKey:nil];
    });
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

核心动画-animationDidStop未调用 的相关文章

随机推荐