核心动画-未调用animationDidStop [英] Core Animation - animationDidStop not called

查看:89
本文介绍了核心动画-未调用animationDidStop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有。

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

in iOS app, Core animation callback don't work.

- (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。
如果我按如下所示更改代码,则会调用完成阻止。

But animationDidStop never be called. If I change the code like as following, completion blocked is called.

- (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?

But I don't want to use CATransaction. Why is not animationDidStop called?

更新:
有一种方法可以设置最终值,如

Update: There is a way to set final value like 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];
}

但动画完成后应完成最终分配。因为图层有多个动态动画,所以我不知道最终值。

But final assignment should be done when the animation is finished. Because there are multiple dynamic animations of layer, so I don't know final value.

推荐答案

我找到了animationDidStop是不叫。

I found the reason why animationDidStop is not called.

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

Because animation was added in loop of other thread,

所以我按如下所示进行修复。

So I fixed like as following.

- (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];
    });
}

这篇关于核心动画-未调用animationDidStop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆