2013-07-17 34 views
4

動畫怪異的行爲我想作一個圓形滑塊,可拖動和動畫。到目前爲止,我已經設法構建滑塊,並使用拖動手柄來移動它並進行動畫處理。有時,動畫出錯(錯誤的方向或最短的方向。我已經子類化一個UIView(將是一個UIControl很快,只是想立刻先拿到動畫)加入了PanGestureRecognizer和若干層的圖紙。圓形滑塊,同時與CA

enter image description here

那麼,如何解決這個怪異的行爲我已經有人可以幫助我在這裏,我要感謝:)

這裏的樣本項目 - >http://cl.ly/2l0O3b1I3U0X

非常感謝!

編輯:

這裏的繪圖代碼:

CALayer *aLayer = [CALayer layer]; 
aLayer.bounds = CGRectMake(0, 0, 170, 170); 
aLayer.position = self.center; 
aLayer.transform = CATransform3DMakeRotation(M_PI_4, 0, 0, 1); 

self.handleHostLayer = [CALayer layer]; 
self.handleHostLayer.bounds = CGRectMake(0, 0, 170, 170); 
self.handleHostLayer.position = CGPointMake(CGRectGetMaxX(aLayer.bounds) - 170/2.0, CGRectGetMaxY(aLayer.bounds) - 170/2.0); 

[aLayer addSublayer:self.handleHostLayer]; 
[self.layer addSublayer:aLayer]; 

self.handle = [CALayer layer]; 
self.handle.bounds = CGRectMake(0, 0, 50, 50); 
self.handle.cornerRadius = 25; 
self.handle.backgroundColor = [UIColor whiteColor].CGColor; 
self.handle.masksToBounds = NO; 
self.handle.shadowOffset = CGSizeMake(3.0, 0.0); 
self.handle.shadowRadius = 0; 
self.handle.shadowOpacity = .15; 
self.handle.shadowColor = [UIColor blackColor].CGColor; 

[self.handleHostLayer addSublayer:self.self.handle]; 

這裏的動畫代碼:

CGFloat handleTarget = ToRad(DEG); 

CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 
rotationAnimation.fromValue = @([[self.handleHostLayer valueForKeyPath:@"transform.rotation"] floatValue]); 
rotationAnimation.toValue = @(handleTarget); 
rotationAnimation.duration = .5; 
rotationAnimation.removedOnCompletion = NO; 
rotationAnimation.fillMode = kCAFillModeForwards; 
rotationAnimation.cumulative = YES; 
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 

[self.handleHostLayer addAnimation:rotationAnimation forKey:@"transform.rotation"]; 
+0

動畫代碼似乎是問題。你只能發佈動畫和可能的圖紙代碼。我不想通過項目來找到它。 –

+0

非常感謝您的幫助!我發佈了上面的繪圖代碼(繪圖在drawRect中完成:) – reiserD

回答

1

OK,我看着你的項目。您的問題是,從角度和角度都不落在0≤ɸ<2π範圍內。

可以確保他們通過添加和刪除2π,直到他們都是該範圍內的事。

CGFloat fromAngle = [[self.handleHostLayer valueForKeyPath:@"transform.rotation"] floatValue]; 
CGFloat toAngle = handleTarget; 

while (fromAngle >= 2.0*M_PI) { fromAngle -= 2*M_PI; } 
while (toAngle >= 2.0*M_PI) { toAngle -= 2*M_PI; } 
while (fromAngle < 0.0)  { fromAngle += 2*M_PI; } 
while (toAngle < 0.0)  { toAngle += 2*M_PI; } 

CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 
rotationAnimation.fromValue = @(fromAngle); 
rotationAnimation.toValue = @(toAngle); 
// As before... 

另一種東西,你可以改變是removeOnCompletion濫用。我在this answer中做了一個很長的解釋,說明爲什麼這樣做不好。

簡而言之:你是不是從你以爲你是動畫,因爲你無意中引入層屬性的值,哪些是你在屏幕上看到的差異值動畫。

跳過該行一起。您也可以跳過累計行,因爲您不重複該動畫。現在,如果您的動畫沒有粘貼:在將動畫添加到動畫之前,將模型值設置爲其最終值。

+0

謝謝。不幸的是我的問題並沒有完全解決。手柄仍在整個圓周滑動。我認爲這與我的旋轉材料有關。手柄是一個圓形層,作爲底層插入到旋轉的矩形層中。如果我旋轉過零,則以rad爲單位的角度是例如5.8 ...層的轉換旋轉在-0.45 ...。所以這可能就是爲什麼它全身而退的原因。但如何解決這個問題..我會試試:) – reiserD

+0

聯合國遺憾的不是。的NSLog(@ 「handleTarget:%F,C:%F」,handleTarget,[[self.handleHostLayer valueForKeyPath:@ 「transform.rotation」]的floatValue]); - > handleTarget:6.178466,c:-0.104720 – reiserD

+0

非常感謝您的努力。我幾乎投降。低劣的圈子的東西。現在它是閃爍的,仍然在整個圈子中移動。不能讓我的頭靠近它。特別是如果它是大約86°F ... – reiserD