在我正在处理的 CALayer 子类中,我有一个要自动设置动画的自定义属性,也就是说,假设该属性称为“myProperty”,我想要以下代码:
[myLayer setMyProperty:newValue];
导致从当前值到“newValue”的平滑动画。
使用覆盖 actionForKey: 和 needsDisplayForKey: 的方法(见下面的代码)我能够让它运行得很好,只需在旧值和新值之间进行插值。
我的问题是我想根据属性的当前值和新值使用稍微不同的动画持续时间或路径(或其他),我无法弄清楚如何从 actionForKey 中获取新值:
提前致谢
@interface ERAnimatablePropertyLayer : CALayer {
float myProperty;
}
@property (nonatomic, assign) float myProperty;
@end
@implementation ERAnimatablePropertyLayer
@dynamic myProperty;
- (void)drawInContext:(CGContextRef)ctx {
... some custom drawing code based on "myProperty"
}
- (id <CAAction>)actionForKey:(NSString *)key {
if ([key isEqualToString:@"myProperty"]) {
CABasicAnimation *theAnimation = [CABasicAnimation animationWithKeyPath:key];
theAnimation.fromValue = [[self presentationLayer] valueForKey:key];
... I want to do something special here, depending on both from and to values...
return theAnimation;
}
return [super actionForKey:key];
}
+ (BOOL)needsDisplayForKey:(NSString *)key {
if ([key isEqualToString:@"myProperty"])
return YES;
return [super needsDisplayForKey:key];
}
@end