1
下のコードはCALayer
のサブクラスです。 animateVal
に電話すると、drawInContext
がアニメーション化されます。それは素晴らしいことです。次のコアアニメーション暗黙アニメーションが機能しないのはなぜですか?
actionForKey
で暗黙のアニメーションに変換しようとしました。 val
プロパティ(layer.val = 10
)に値を割り当てると、すぐにval
が変更され、val
という同じ値でdrawInContext
が繰り返し呼び出されます。
私は間違っていますか?
@interface MyLayer : CALayer
@property (nonatomic, assign) CGFloat val;
- (void)animateVal:(CGFloat)val;
@end
@implementation MyLayer
@dynamic val;
+ (BOOL)needsDisplayForKey:(NSString *)key {
return [key isEqualToString:@"val"] || [super needsDisplayForKey:key];
}
- (id<CAAction>)actionForKey:(NSString *)event {
if ([event isEqualToString:@"val"]) {
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"val"];
anim.duration = 3.0;
return anim;
}
return [super actionForKey:event];
}
- (void)animateVal:(CGFloat)val {
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"val"];
anim.toValue = [NSNumber numberWithFloat:val];
anim.duration = 3.0;
[self addAnimation:anim forKey:nil];
}
- (void)drawInContext:(CGContextRef)ctx {
CGContextAddRect(ctx, CGRectMake(10, 10, self.val, self.val));
CGContextSetFillColorWithColor(ctx, [UIColor greenColor].CGColor);
CGContextFillPath(ctx);
}
@end