2014-11-08 141 views
2

我有一個視圖,其中我有多個水平線(如horzontal撥號)。我想動畫水平運動。它應該是這樣的Ios畫線動畫

編輯:

Horizontal Animation

enter image description here

enter image description here

我怎樣才能做到這一點?據我瞭解,我不能使用CoreAnimation。此外,我不能模擬不同類型的速度。 updatemethod如何工作?我是否改變抽籤時間或距離?

我知道我可以用scrollview解決這個問題,但我不想使用它。

謝謝!

+0

我不完全明白你想要做什麼。爲什麼你不能使用核心動畫? – 2014-11-08 10:31:10

+0

也許我不瞭解Core Animation。據我所知,它激發了觀點的運動,而不是內在。我的想法是顯示撥號盤的邊緣,當撥號盤旋轉時,線條移動。因此,線條必須始終在不動的區域內重複。 – Jan 2014-11-08 10:38:40

+0

你能否包含一張圖片顯示你正在嘗試做什麼(最好多一幀(理解動畫的意思)? – 2014-11-08 15:45:42

回答

8

如果我已經理解你正在嘗試做什麼,那麼我沒有理由不能使用Core Animation來做到這一點。

如果您正在移動的圖案非常簡單,那麼您可以在CAShapeLayer上使用線條圖案來繪製圖案。我建立了我的層,使其從邊界高度得到的線條寬度和形狀層的路徑從層的邊界得到它的起點和終點:

CAShapeLayer *lineLayer = [CAShapeLayer layer]; 
lineLayer.bounds = CGRectMake(0, 0, 200, 60); 
lineLayer.position = self.view.center; 

lineLayer.strokeColor = [UIColor blackColor].CGColor; 
lineLayer.lineDashPattern = @[@5, @2, @2, @2, @2, @2, @2, @2, @2, @2]; 
lineLayer.lineWidth = CGRectGetHeight(lineLayer.bounds); 

UIBezierPath *linePath = [UIBezierPath bezierPath]; 
[linePath moveToPoint:CGPointMake(0, CGRectGetMidY(lineLayer.bounds))]; 
[linePath addLineToPoint:CGPointMake(CGRectGetMaxX(lineLayer.bounds), CGRectGetMidY(lineLayer.bounds))]; 
lineLayer.path = linePath.CGPath; 

[self.view.layer addSublayer:lineLayer]; 

這將產生的靜電繪圖模式。代碼中的虛線圖案是線條顯示位置和不顯示位置的交替段的長度。

enter image description here

隨着繪製的路徑,我通過改變線的破折號「相」做了動畫(在一個方向或另一個方向移動它們)。使之看起來像圖案是平滑且連續地移動我創建了一個線性的,重複的動畫,通過其整個長度移位的圖案:

NSNumber *totalDashLenght = [lineLayer.lineDashPattern valueForKeyPath:@"@sum.self"]; // KVC is awesome :) 

CABasicAnimation *animatePhase = [CABasicAnimation animationWithKeyPath:@"lineDashPhase"]; 
animatePhase.byValue = totalDashLenght; // using byValue means that even if the layer has a shifted phase, it will shift on top of that. 
animatePhase.duration = 3.0; 
animatePhase.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 
animatePhase.repeatCount = INFINITY; 

[lineLayer addAnimation:animatePhase forKey:@"marching ants"]; 

動畫線看起來像這樣:

enter image description here

如果您想讓不同的線條以不同的速度動畫,請更改動畫的duration。此值表示動畫移動一個完整階段所需的時間。

+0

哇,所以我完全沒有得到Core Animation。只是想知道..因爲我想用它作爲一個控件,我如何將它移動到我的手指下? – Jan 2014-11-08 18:16:25

+0

您可能可以使用圖層的'timeOffset'來控制動畫的時間(就像在[這個答案]中一樣)(http://stackoverflow.com/a/18683845/608157))你必須弄清楚拖動手指和動畫階段之間的數學轉換。這取決於一個階段的長度和動畫的持續時間。 – 2014-11-08 19:59:54