为什么动画自定义的CALayer属性导致其他性能是零动画过程中? [英] Why animating custom CALayer properties causes other properties to be nil during animation?

查看:181
本文介绍了为什么动画自定义的CALayer属性导致其他性能是零动画过程中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的CALayer(比如CircleLayer),含自定义属性(半径和色调)。该层呈现本身在其drawInContext:方法

I have a custom CALayer (say CircleLayer), containing custom properties (radius and tint). The layer renders itself in its drawInContext: method.

- (void)drawInContext:(CGContextRef)ctx {
    NSLog(@"Drawing layer, tint is %@, radius is %@", self.tint, self.radius);

    CGPoint centerPoint = CGPointMake(CGRectGetWidth(self.bounds)/2, CGRectGetHeight(self.bounds)/2);

    CGContextMoveToPoint(ctx, centerPoint.x, centerPoint.y);
    CGContextAddArc(ctx, centerPoint.x, centerPoint.y, [self.radius doubleValue], radians(0), radians(360), 0);
    CGContextClosePath(ctx);

    /* Filling it */
    CGContextSetFillColorWithColor(ctx, self.tint.CGColor);
    CGContextFillPath(ctx); 
}

我想半径是动画,所以我已经实现了

I want the radius to be animatable so I've implemented

+ (BOOL)needsDisplayForKey:(NSString *)key {
    if ([key isEqualToString:@"radius"]) {
        return YES;
    }
    return [super needsDisplayForKey:key];
}

和动画是这样进行的:

CABasicAnimation *theAnimation=[CABasicAnimation animationWithKeyPath:@"radius"];
theAnimation.duration=2.0;
theAnimation.fromValue=[NSNumber numberWithDouble:100.0];
theAnimation.toValue=[NSNumber numberWithDouble:50.0];
theAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

[circleLayer addAnimation:theAnimation forKey:@"animateRadius"];

circleLayer.radius = [NSNumber numberWithDouble:50.0];

drawInContext:动画期间预计将重绘圈被调用,但是色调就会被设置为零的动画开始,再返回其原始值,当动画结束

drawInContext: gets called as expected during the animation to redraw the circle, however the tint is set to nil as soon as the animation starts and gets back to its original value when the animation ends.

我认为,如果我想动画自定义属性,并希望其他属性在动画过程中保持自己的价值,我有动画他们太多,我觉得这不是方便的。

I've concluded that if I want to animate a custom property and want other properties to keep their value during the animation, I have to animate them too, which I find not being convenient at all.

的目的不是要放大/缩小了一圈,我知道我可以使用转换这一点。这只是一个简单的例子来说明动画一个自定义属性,而无需动画所有其他的人的问题。

The purpose is not to grow/shrink a circle, I know I can use transformation for this. It is only to illustrate with a simple example the problem of animating a single custom property without having to animate all the other ones.

我做了说明这个问题,你可以在这里找到一个简单的项目:
说明问题示例项目

I've made a simple project illustrating the issue, which you can find here: Sample project illustrating the issue

有可能是东西,我没有对CoreAnimation是如何工作的,我已经进行密集搜索得到,但我坚持没有线索。任何人都知道?

There is probably something I didn't get on how CoreAnimation works, I've performed intensive searching but I'm stuck with no clue. Anyone knows?

推荐答案

如果我理解正确你的问题,它是这样的。当您添加一个动画一个CALayer的,它将使用 initWithLayer那层所谓的presentation复制:。在presentation层包含每个动画帧动画的实际状态,而原来层的最终状态。与动画自己的属性问题是CALayer的不复制它们都在 initWithLayer:。如果这是你的情况,你应该重写 initWithLayer:并设置您需要的动画,那就是,无论是色调和半径的所有属性。

If I understood your question correctly, it goes like this. When you add an animation to a CALayer, it creates a so-called presentation copy of that layer using initWithLayer:. The presentation layer contains actual animated state for each animation frame, while the original layer has the final state. The problem with animating your own properties is that CALayer does not copy them all in initWithLayer:. If that's your case, your should override initWithLayer: and set up all the properties you need for animation, that is, both tint and radius.

这篇关于为什么动画自定义的CALayer属性导致其他性能是零动画过程中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆