2014-10-01 80 views
2

我试图动画绘制对象,比如我画这种方法行:如何在Objective C中绘制线条的动画?

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原样,但我不知道该怎么做。我的方法好吗?有没有更好的方法来做到这一点?

+0

可能重复[如何动画CAShapeLayer路径和填充颜色(http://stackoverflow.com/questions/15651717/how-to-animate-cashapelayer-path-and- fillcolor) – 2014-10-01 13:15:08

+0

这是动画绘制的路径,而不是动画后,它已经绘制(这是我想要实现的)。 虽然谢谢! :d – Shamy 2014-10-01 14:35:20

回答

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;