2016-01-14 93 views
0

我想創建一條貝塞爾曲線的圓弧,然後爲其設置動畫以減小曲線的半徑。貝塞爾曲線的動畫大小

目前,我有下面的代碼繪製一個UIView我的貝塞爾曲線:

UIView * curveView = [[UIView alloc] initWithFrame:CGRectMake(50, 550, 100, 100)]; 
curveView.backgroundColor = [UIColor orangeColor]; 

UIBezierPath * aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) 
                radius:75 
               startAngle:0 
                endAngle:2 * M_PI 
                clockwise:YES]; 

CAShapeLayer * shapeLayer = [CAShapeLayer layer]; 
shapeLayer.path = aPath.CGPath; 
shapeLayer.fillColor = [UIColor colorWithRed:.5 green:1 blue:.5 alpha:1].CGColor; 
shapeLayer.strokeColor = [UIColor blackColor].CGColor; 
shapeLayer.lineWidth = 2; 

[curveView.layer addSublayer:shapeLayer]; 

[self.view addSubview:curveView]; 

這增加了一個圓形的貝塞爾曲線的視圖預期。我遇到的問題是曲線在一段時間內縮小。

如果我使用UIViews相反,我可能會使用的限制,但我似乎無法找到任何資源,幫助我在這種情況下使用它們。

看了很多關於Stackoverflow上不同問題的答案,似乎我可能需要使用CABasicAnimation,但問題主要是爲線條的曲線而不是線條的長度設置動畫。作爲用於描繪線的方法(開始點,一系列的路徑,結束點)似乎從繪製的圓的非常不同

此問題是困難的。這意味着我不知道我正在尋找的過程是相同的還是不同的。一個答案here似乎有一個動畫線的解決方案,但我創建圓的代碼不使用開始或結束點,這意味着它沒有太大的幫助。

我想過使用單獨的圓弧創建圈子,然後改變其啓動點和控制點,但這些弧似乎並沒有成爲完美的圓形,並更多地用於自定義的波浪線。有一個here的例子,但它不適用於iOS。

回答

1

path是CAShapeLayer的動畫屬性。如果您創建第二個較小版本的圓形路徑,則可以在這兩條路徑之間進行動畫製作,就像處理其他任何動畫屬性一樣。

UIBezierPath *smallerPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:25 /* note different radius */ startAngle:0 endAngle:2 * M_PI clockwise:YES]; 

CABasicAnimation *shrinkAnimation = [CABasicAnimation animationWithKeyPath:@"path"]; 
shrinkAnimation.fromValue = (id)aPath.CGPath; 
shrinkAnimation.toValue = (id)smallerPath.CGPath; 
shrinkAnimation.duration = 1; 
[shapeLayer addAnimation:shrinkAnimation forKey:@"shrink"];