如何在Objective C中为绘制线设置动画?

时间:2014-10-01 12:40:56

标签: objective-c uibezierpath cgpoint uianimation cashapelayer

我正在尝试动画绘制的对象,例如我用这种方法画了一条线:

    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(Center.x, Center.y)];
    [path addLineToPoint:CGPointMake(Point.x,Point.y)];

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = [path CGPath];
    shapeLayer.strokeColor = [[UIColor whiteColor] CGColor];
    shapeLayer.lineWidth = 1.5;
    shapeLayer.fillColor = [[UIColor clearColor] CGColor];

    [self.view.layer addSublayer:shapeLayer];

过了一段时间,我需要为该行设置动画以使其具有不同的Point,但保持Center不变,但我不知道该怎么做。我的做法好吗?有没有更好的方法呢?

1 个答案:

答案 0 :(得分:2)

解决了它:

    CABasicAnimation *morph = [CABasicAnimation animationWithKeyPath:@"path"];
    morph.duration = 1.0;
    UIBezierPath *path5 = [UIBezierPath bezierPath];
    [path5 moveToPoint:CGPointMake(P22.x+5, P22.y+5)];
    [path5 addLineToPoint:CGPointMake(P21.x,P21.y)];
    morph.fromValue = (id) path.CGPath;
    morph.toValue = (id) path5.CGPath;

    [shapeLayer addAnimation:morph forKey:nil];

    //next line to update the shapeLayer's new path
    shapeLayer.path = path5.CGPath;