2011-09-01 109 views
14

我在嘗試旋轉UIImageview時出現問題,內部持有球的圖像。我希望這個球在中軸線上連續旋轉。 我嘗試過使用CGAffineTransform,但它沒有奏效。連續旋轉UIImageView

請幫忙!

+0

你需要旋轉'UIImageView'球裏面?如果是的話考慮使用'UIImageView'本身的動畫方法 – Saphrosit

回答

4

如果您使用轉換,因爲它應該工作:

itemToRotate.transform = CGAffineTransformRotate(itemToRotate.transform, currentAngle); 

我已經上傳了一個有效的解決方案的一些sample code。添加你的邏輯自動旋轉它...

+0

鏈接不要woking:| –

+0

哎呀......我已經通過CloudApp帳戶刪除了此處的託管XO – tipycalFlow

80

這可能是一個老Q,但它接近該主題的搜索結果的頂部。這裏有一個更添油加醋的解決方案:(確保導入QuartzCore/QuartzCore.h)

CABasicAnimation *rotation; 
rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 
rotation.fromValue = [NSNumber numberWithFloat:0]; 
rotation.toValue = [NSNumber numberWithFloat:(2*M_PI)]; 
rotation.duration = 1.1; // Speed 
rotation.repeatCount = HUGE_VALF; // Repeat forever. Can be a finite number. 
[yourView.layer addAnimation:rotation forKey:@"Spin"]; 

然後,停止刪除/重置動畫:(見如何停止就地評論)

[yourView.layer removeAnimationForKey:@"Spin"]; 

在SWIFT:

let rotation = CABasicAnimation(keyPath: "transform.rotation") 
rotation.fromValue = 0 
rotation.toValue = 2 * M_PI 
rotation.duration = 1.1 
rotation.repeatCount = Float.infinity 
view.layer.addAnimation(rotation, forKey: "Spin") 
+0

什麼是創業板。謝謝 –

+0

@Christopher Swasey在動畫中間移除動畫鍵會將視圖渲染爲原始狀態,而不是轉換狀態。任何方式來保存轉換? – tGilani

+1

@tGilani,爲了保存轉換,只需在設置CABasicAnimation時添加'rotation.removedOnCompletion = NO;'。這將使動畫的表示層可見。有關iOS動畫中使用的不同圖層的更多信息,我發現這篇文章非常有用:[鏈接](http://stackoverflow.com/a/11515749/1114876)。 – adamup