我有一个CALayer
子类,其浮动animAngle
属性标记为@dynamic
。我为子类实现了方法actionForKey
,initWithLayer
,needsDisplayForKey
和drawInContext
。
actionForKey
的定义如下
- (id<CAAction>)actionForKey:(NString *)event {
if([event isEqualToString:@"animAngle"]) {
return [self animationForKey:event];
}
return [super actionForKey:event];
}
和
- (CABasicAnimation *)animationForKey:(NSString *)key
{
NSString *animValue = [[self presentationLayer] valueForKey:key];// Logs as 0
CABasicAnimation *anim;
if([key isEqualToString:@"animAngle"]) {
anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
anim.repeatCount = HUGE_VAL;
anim.autoreverses = YES;
//anim.fromValue = [[self presentationLayer] valueForKey:key]; // setting animation value from layer property as in here does not work.
anim.fromValue = [NSNumber numberWithFloat:0.5f]; // This works
}
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
anim.duration = 0.11;
return anim;
}
在其他课程中:
myCASublayer.animAngle = 0.5f;
以某种方式返回的CABasicAnimation
无法正确使用图层“animAngle
”属性。
我在这里可能做错了什么?
答案 0 :(得分:1)
如果animAngle
是@property
,则必须为此属性指定accessors
。
将属性标记为@dynamic
时,这意味着您将在运行时动态提供这些方法的实现。因此,如果您不为属性提供访问器,则无法访问它。
答案 1 :(得分:1)
CocoaHeads Session: Rob Napier on Animating Custom Layer Properties是关于自定义动画的精彩演示。
CALayers讨厌画画;)