【问题标题】:How reliable [CALayer animationForKey:] is?[CALayer animationForKey:] 有多可靠?
【发布时间】:2011-11-09 22:06:20
【问题描述】:

我有一个CALayer 的子类,称为MyLayer

@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: 不返回@ 987654329@。行为是半随机的......大多数时候它都按预期进行。但有时在我开始收到nil-s 之前需要 1-2 秒。

如果我将代理设置为动画animation.delegate = self 并实现 animationDidStop:finished:,这种奇怪的行为消失

有人也遇到过这种情况吗?

【问题讨论】:

  • 你在主线程上用animationForKey检查吗?
  • 是的,主 gui 线程中的所有内容...
  • 我看到了同样的行为,对于你来说,添加一个委托和animationDidStop:finished: 方法会得到预期的行为。非常奇怪。核心动画错误?还有其他解决方案吗?

标签: iphone core-animation calayer cakeyframeanimation


【解决方案1】:

声明@dynamic 会导致CALayer 为您实现访问器方法(请参阅Properties on CALayer subclass aren't getting observed by CATransactionCALayer and CAAnimation's dynamic resolution of unimplemented property accessors)。因此,您可能只是看到在调用哪个版本的“忙”时感到困惑。你为什么首先声明“@dynamic”?

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-27
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 2011-04-23
    • 2020-07-18
    相关资源
    最近更新 更多