动画基于路径属性CAShapeLayer的子类自定义属性 [英] Animate a CAShapeLayer's subclass custom property based on the path property

查看:221
本文介绍了动画基于路径属性CAShapeLayer的子类自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CAShapeLayer子类有两个属性,一个的startPoint和端点。这些特性不直接吸入到上下文而是用于改变的路径属性,类似变换,位置和界限性能

I've got a CAShapeLayer subclass which features two properties, a startPoint and an endPoint. These properties aren't directly drawn into the context but rather used to alter the path property, similar to the transform, position and bounds properties.

下面是整层的code

Here's the code of the whole layer

-(void)setStartPoint:(CGPoint)startPoint {
    if (!CGPointEqualToPoint(_startPoint, startPoint)) {
        _startPoint = startPoint;

        [self reloadPath];
    }
}

-(void)setEndPoint:(CGPoint)endPoint {
    if (!CGPointEqualToPoint(_endPoint, endPoint)) {
        _endPoint = endPoint;

        [self reloadPath];
    }
}

-(id)initWithLayer:(CRPathLayer*)layer {
    self = [super initWithLayer:layer];
    if (self && [layer isKindOfClass:[CRPathLayer class]]) {
        self.startPoint = layer.startPoint;
        self.endPoint = layer.endPoint;
    }

    return self;
}

+(BOOL)needsDisplayForKey:(NSString *)key {
    if ([key isEqualToString:NSStringFromSelector(@selector(startPoint))] || [key isEqualToString:NSStringFromSelector(@selector(endPoint))]) {
        return YES;
    }

    return [super needsDisplayForKey:key];
}

-(void)reloadPath {
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, self.startPoint.x, self.startPoint.y);
    CGPathAddLineToPoint(path, NULL, self.endPoint.x, self.endPoint.y);

    self.path = path;
    CGPathRelease(path);
}

但是有一个问题。我需要动画两个新特性(开始 - 或端点)中的一个。我知道我可以只直接动画路径财产的事实,但是这不是我想要达到的目标。
我明显缺少在执行一些东西。当我尝试使用CABasicAnimation没什么动画它发生在所有。

However there's a problem. I need to animate one of the two new properties (start- or endPoint). I'm aware of the fact that I could just animate the path property directly, however this is not what I want to achieve. I'm obviously missing something in the implementation. When I try to animate it using a CABasicAnimation nothing happens at all.

任何人可以帮助我在这里?
先谢谢了。

Can anybody help me out here? Thanks in advance.

推荐答案

你缺少的部分是 actionForKey:方法(和而路径是动画,它不隐式动画(即它不会动画只是因为属性的变化))。

The piece you are missing is the actionForKey: method (and that while the path is animatable, it's not implicitly animatable (i.e. it won't animate just because the property changes)).

您所要做的就是为路径提供一个动作(动画较普通的词语)时:起点或终点的变化(或者只是使该路径隐式动画的这样设置一个新的路径导致动画,他们真正做同样的事情)。

What you have to do is to supply an action (a more general term for an animation) for the path whenever the startPoint or endPoint changes (or just make the path implicitly animatable so that setting a new path causes an animation, they really do the same thing).

这可能看起来是这样的(我在浏览器中输入了这一点,所以它可能不会编译):

It could looks something like this (I typed this out in the browser so it may not compile):

- (id <CAAction>)actionForKey:(NSString *)key 
{
    if ([key isEqualToString:@"startPoint"] || [key isEqualToString:@"endPoint")]) {
        CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"path"];
        anim.fromValue = [self.presentationLayer valueFoKey:@"path"];
        return animo;
    }

    return [super valueForKey:key];
}

这篇关于动画基于路径属性CAShapeLayer的子类自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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