[CALayeranimationForKey:] 有多可靠?
我有一个名为 MyLayer
的 CALayer
子类:
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
声明 @dynamic 会导致 CALayer 为您实现访问器方法(请参阅 CATransaction 和 CALayer 和 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.