[CALayeranimationForKey:] 有多可靠?

发布于 2024-12-14 08:22:28 字数 1087 浏览 2 评论 0原文

我有一个名为 MyLayerCALayer 子类:

@interface MyLayer : CALayer
@property (nonatomic,readonly) BOOL busy;
-(void)performTask1;
-(void)performTask2;
-(void)performTask3;
@end

performTask* 函数中,我说:

-(void)performTask*
{
    CAKeyframeAnimation *animation = [...];
    ...
    animation.removedOnCompletion = YES; // "YES" is default anyway
    ...
    [self addAnimation:animation
                forKey:TASK*_KEY];
}

busy 属性用于调用者并像这样实现:

@dynamic busy;
-(BOOL)busy
{
        // i.e. for some specific ids
    return ([self animationForKey:TASK1_KEY] != nil ||
            [self animationForKey:TASK3_KEY] != nil);
}

我看到这种方法并不可靠...我可以在屏幕上看到动画完成(没有任何东西在移动等),而animationForKey : 不返回。这种行为是半随机的……大多数时候它会按预期进行。但有时需要 1-2 秒才能开始得到 nil-s。

如果我将委托设置为动画 animation.delegate = self 并实现 animationDidStop:finished:消失 >。

有人也经历过这个吗?

I have a subclass of CALayer called MyLayer:

@interface MyLayer : CALayer
@property (nonatomic,readonly) BOOL busy;
-(void)performTask1;
-(void)performTask2;
-(void)performTask3;
@end

In performTask* functions I say:

-(void)performTask*
{
    CAKeyframeAnimation *animation = [...];
    ...
    animation.removedOnCompletion = YES; // "YES" is default anyway
    ...
    [self addAnimation:animation
                forKey:TASK*_KEY];
}

The busy property is for the caller and implemented like this:

@dynamic busy;
-(BOOL)busy
{
        // i.e. for some specific ids
    return ([self animationForKey:TASK1_KEY] != nil ||
            [self animationForKey:TASK3_KEY] != nil);
}

What I see is that this approach is not reliable... I can see on the screen that animation is finished (nothing is moving, etc.), while animationForKey: does NOT return nil. The behavior is semi-random... Most of times it goes as expected. But sometimes it takes 1-2 seconds before I start getting nil-s.

This weird behavior disappears, if I set a delegate to animation animation.delegate = self and implement animationDidStop:finished:.

Someone experienced this as well?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉 2024-12-21 08:22:28

声明 @dynamic 会导致 CALayer 为您实现访问器方法(请参阅 CATransactionCALayer 和 CAAnimation 未实现的属性访问器的动态解析)。因此,您可能只是在调用哪个版本的“busy”时感到困惑。你为什么首先声明“@dynamic”?

AnimationForKey 也可能会发生一些奇怪的事情,但我会尝试先删除“@dynamic”。

Declaring @dynamic causes CALayer to implement the accessor method for you (see Properties on CALayer subclass aren't getting observed by CATransaction and CALayer and CAAnimation's dynamic resolution of unimplemented property accessors). So it's possible that you are just seeing confusion in which version of "busy" is getting called. Why did you declare "@dynamic" in the first place?

There could also be something funky happening with animationForKey, but I'd try removing the "@dynamic" first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文