gpt4 book ai didi

ios - 为什么下面的 Core Animation 隐式动画不起作用?

转载 作者:行者123 更新时间:2023-11-29 13:41:48 24 4
gpt4 key购买 nike

下面的代码是CALayer 的子类。如果我调用 animateVal,则 drawInContext 是动画的。那太棒了。

我试图用 actionForKey 把它变成隐式动画。如果我为 val 属性赋值 (layer.val = 10),val 会立即更改并且 drawInContext 以相同的 val 值反复调用,这是我刚刚分配的值,一遍又一遍。

我做错了什么?

@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

最佳答案

哎呀。

解决方案是在返回操作时设置fromValue

    anim.fromValue = [[self presentationLayer] valueForKey:event];

关于ios - 为什么下面的 Core Animation 隐式动画不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8891301/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com