7

在我正在处理的 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
4

3 回答 3

2

您需要避免为要设置动画的属性自定义 getter 和 setter。

覆盖didChangeValueForKey:方法。使用它来设置要设置动画的属性的模型值。

不要在动作动画上设置 toValue。

@interface MyLayer: CALayer

    @property ( nonatomic ) NSUInteger state;

@end

-

@implementation MyLayer

@dynamic state;

- (id<CAAction>)actionForKey: (NSString *)key {

    if( [key isEqualToString: @"state"] )
    {
        CABasicAnimation * bgAnimation = [CABasicAnimation animationWithKeyPath: @"backgroundColor"];
        bgAnimation.fromValue = [self.presentationLayer backgroundColor];
        bgAnimation.duration  = 0.4;

        return bgAnimation;
    }

    return [super actionForKey: key];
}

- (void)didChangeValueForKey: (NSString *)key {

    if( [key isEqualToString: @"state"] )
    {
        const NSUInteger state = [self valueForKey: key];
        UIColor * newBackgroundColor;

        switch (state) 
        {
            case 0: 
                newBackgroundColor = [UIColor redColor]; 
                break;

            case 1: 
                newBackgroundColor = [UIColor blueColor]; 
                break;

            case 2: 
                newBackgroundColor = [UIColor greenColor]; 
                break;

            default: 
                newBackgroundColor = [UIColor purpleColor]; 
        }

        self.backgroundColor = newBackgroundColor.CGColor;
    }

    [super didChangeValueForKey: key];
}

@end
于 2013-05-05T05:41:13.003 回答
2

Core AnimationactionForKey:在更新属性值之前调用。它在通过发送更新属性值后运行该操作runActionForKey:object:arguments:。执行CAAnimation只是runActionForKey:object:arguments:调用[object addAnimation:self forKey:key]

除了从 中返回动画actionForKey:,您还可以返回一个CAAction,它在运行时创建并安装动画。像这样的东西:

@interface MyAction: NSObject <CAAction>
@property (nonatomic, strong) id priorValue;
@end

@implementation MyAction

- (void)runActionForKey:(NSString *)key object:(id)anObject arguments:(NSDictionary *)dict {
    ERAnimatablePropertyLayer *layer = anObject;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:key];
    id newValue = [layer valueForKey:key];
    // Set up animation using self.priorValue and newValue to determine
    // fromValue and toValue. You could use a CAKeyframeAnimation instead of
    // a CABasicAnimation.
    [layer addAnimation:animation forKey:key];
}

@end

@implementation ERAnimatablePropertyLayer

- (id <CAAction>)actionForKey:(NSString *)key {
    if ([key isEqualToString:@"myProperty"]) {
        MyAction *action = [[MyAction alloc] init];
        action.priorValue = [self valueForKey:key];
        return action;
    }
    return [super actionForKey:key];
}

您可以在此答案中找到类似技术的工作示例(在 Swift 中,动画cornerRadius)。

于 2016-03-01T04:53:57.080 回答
1

您可以将旧值和新值存储在 CATransaction 中。

-(void)setMyProperty:(float)value
{
    NSNumber *fromValue = [NSNumber numberWithFloat:myProperty];
    [CATransaction setValue:fromValue forKey:@"myPropertyFromValue"];

    myProperty = value;

    NSNumber *toValue = [NSNumber numberWithFloat:myProperty];
    [CATransaction setValue:toValue forKey:@"myPropertyToValue"];
}

- (id <CAAction>)actionForKey:(NSString *)key {
    if ([key isEqualToString:@"myProperty"]) {
        CABasicAnimation *theAnimation = [CABasicAnimation animationWithKeyPath:key];
        theAnimation.fromValue = [[self presentationLayer] valueForKey:key];
        theAnimation.toValue = [CATransaction objectForKey:@"myPropertyToValue"];

        // here you do something special.
    }

    return [super actionForKey:key];
}
于 2012-06-05T18:01:04.027 回答