2010-09-16 67 views
1

我試圖讓這當用戶拖動輪目標C - 旋轉使用輪弧度

轉一圈圖像這是我的代碼:

CABasicAnimation *rotationAnimation; 
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
rotationAnimation.toValue = [NSNumber numberWithFloat:angleRadians]; 
rotationAnimation.duration = 10; 
rotationAnimation.cumulative = YES; 
rotationAnimation.repeatCount = 1.0; 
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 
[self.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; 

我有,雖然主要的問題,圖像完成時正在重置。

請幫忙嗎?

回答

4

我認爲你正在接近這個問題錯了。核心動畫主要用於爲視覺轉換,淡入淡出等瞬態效果製作動畫。對圖層進行動畫處理後,CA放棄其工作值並恢復圖層的原始狀態,這就是圖像重置的原因。雖然毫無疑問可以使用CA來做你想做的事情,但我認爲這是艱難的做法。

使圖像跟蹤用戶的手指,我建議乾脆計算從當前觸摸位置需要的角度,並轉換圖像用於那個角度每當觸摸位置的變化: -

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    double angle = /* calculate your angle in radians here */; 
    imageView.transform = CGAffineTransformMakeRotation(angle); 
} 
+0

謝謝一百萬! – Lilz 2010-09-17 12:49:25