2014-10-01 79 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;