【发布时间】:2014-08-27 04:19:33
【问题描述】:
我已经定义了CALayer 的一个子类,它具有here 所讨论的动画属性。我现在想向该层添加另一个(非动画)属性以支持其内部簿记。
我在drawInContext: 中设置了新属性的值,但我发现在下一次调用时它总是重置为0。是不是因为 Core Animation 假设这个属性也是用于动画的,并且它在没有进一步说明的情况下将其值“动画化”为常量 0?无论如何,我怎样才能将真正不可动画的属性添加到CALayer 的子类?
我找到了一个初步的解决方法,它使用全局 CGFloat _property 而不是 @property (assign) CGFloat property,但更喜欢使用普通属性语法。
更新 1
这就是我尝试在MyLayer.m 中定义属性的方式:
@interface MyLayer()
@property (assign) CGFloat property;
@end
这就是我在drawInContext: 末尾为其赋值的方式:
self.property = nonZero;
该属性是例如在drawInContext: 的开头阅读如下:
NSLog(@"property=%f", self.property);
更新 2
也许这是导致问题的原因(代码继承自 this 示例)?
- (id)actionForKey:(NSString *) aKey {
if ([aKey isEqualToString:@"someAnimatableProperty"]) {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:aKey];
animation.fromValue = [self.presentationLayer valueForKey:aKey];
return animation;
}
return [super actionForKey:aKey]; // also applies to my "property"
}
【问题讨论】:
-
你能说明一下这个属性是如何定义的,它的值是如何设置的以及它们在哪里被读取?
-
@DavidRönnqvist 完成。
-
您是否尝试在动画期间读取值?
-
你在
-initWithLayer:做什么?
标签: objective-c ios7 core-animation