核心动画 - 未调用animationDidStop

jei*_*oft 3 iphone core-animation ios

那里.

在iOS应用程序中,Core动画回调不起作用.

- (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"];
}
Run Code Online (Sandbox Code Playgroud)

但是从未调用过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];
}
Run Code Online (Sandbox Code Playgroud)

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

更新: 有一种方法可以将最终值设置为as

- (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];
}
Run Code Online (Sandbox Code Playgroud)

但是最终的作业应该在动画结束时完成.因为图层有多个动态动画,所以我不知道最终值.

jei*_*oft 7

我找到了没有调用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];
    });
}
Run Code Online (Sandbox Code Playgroud)