UIView無限360度旋轉動畫?我正在嘗試旋轉UIImageView360度,并在線查看了幾個教程。我可以讓他們都沒有工作,沒有UIView停止,或跳到一個新的位置。我怎樣才能做到這一點?我嘗試過的最新事情是:[UIView animateWithDuration:1.0
delay:0.0
options:0
animations:^{
imageToMove.transform = CGAffineTransformMakeRotation(M_PI);
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];但是如果我使用2 * pi,它根本不會移動(因為它是相同的位置)。如果我嘗試做pi(180度),它可以工作,但如果我再次調用該方法,它會向后旋轉。編輯:[UIView animateWithDuration:1.0
delay:0.0
options:0
animations:^{
[UIView setAnimationRepeatCount:HUGE_VALF];
[UIView setAnimationBeginsFromCurrentState:YES];
imageToMove.transform = CGAffineTransformMakeRotation(M_PI);
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];也不起作用。它會轉到180度數,暫停一瞬間,然后在重新0開始之前重新設置為度數。
3 回答

有只小跳蛙
TA貢獻1824條經驗 獲得超8個贊
找到一個方法(我修改了一下),對我來說非常有效:iphone UIImageView旋轉
#import <QuartzCore/QuartzCore.h>- (void) runSpinAnimationOnView:(UIView*)view duration:(CGFloat)duration rotations:(CGFloat)rotations repeat:(float)repeat { CABasicAnimation* rotationAnimation; rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ]; rotationAnimation.duration = duration; rotationAnimation.cumulative = YES; rotationAnimation.repeatCount = repeat ? HUGE_VALF : 0; [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];}

慕娘9325324
TA貢獻1783條經驗 獲得超4個贊
我發現他的代碼并不是我所需要的。options
我相信默認值是給你UIViewAnimationOptionCurveEaseInOut
,在連續動畫中看起來不正確。此外,我添加了一個檢查,以便我可以在需要時(不是無限,但是無限期)在一個偶數四分之一圈停止我的動畫,并使加速度在前90度加速,并在最后90度減速(在要求停止后):
// an ivar for your class:BOOL animating;- (void)spinWithOptions:(UIViewAnimationOptions)options { // this spin completes 360 degrees every 2 seconds [UIView animateWithDuration:0.5 delay:0 options:options animations:^{ self.imageToMove.transform = CGAffineTransformRotate(imageToMove.transform, M_PI / 2); } completion:^(BOOL finished) { if (finished) { if (animating) { // if flag still set, keep spinning with constant speed [self spinWithOptions: UIViewAnimationOptionCurveLinear]; } else if (options != UIViewAnimationOptionCurveEaseOut) { // one last spin, with deceleration [self spinWithOptions: UIViewAnimationOptionCurveEaseOut]; } } }];}- (void)startSpin { if (!animating) { animating = YES; [self spinWithOptions: UIViewAnimationOptionCurveEaseIn]; }}- (void)stopSpin { // set the flag to stop spinning after one last 90 degree increment animating = NO;}
更新
我添加了處理再次開始旋轉的請求的能力(startSpin
),而之前的旋轉正在逐漸減少(完成)。Github上的示例項目。

慕森卡
TA貢獻1806條經驗 獲得超8個贊
在Swift中,您可以使用以下代碼進行無限循環:
斯威夫特4
extension UIView { private static let kRotationAnimationKey = "rotationanimationkey" func rotate(duration: Double = 1) { if layer.animation(forKey: UIView.kRotationAnimationKey) == nil { let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation") rotationAnimation.fromValue = 0.0 rotationAnimation.toValue = Float.pi * 2.0 rotationAnimation.duration = duration rotationAnimation.repeatCount = Float.infinity layer.add(rotationAnimation, forKey: UIView.kRotationAnimationKey) } } func stopRotating() { if layer.animation(forKey: UIView.kRotationAnimationKey) != nil { layer.removeAnimation(forKey: UIView.kRotationAnimationKey) } }}
斯威夫特3
let kRotationAnimationKey = "com.myapplication.rotationanimationkey" // Any keyfunc rotateView(view: UIView, duration: Double = 1) { if view.layer.animationForKey(kRotationAnimationKey) == nil { let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation") rotationAnimation.fromValue = 0.0 rotationAnimation.toValue = Float(M_PI * 2.0) rotationAnimation.duration = duration rotationAnimation.repeatCount = Float.infinity view.layer.addAnimation(rotationAnimation, forKey: kRotationAnimationKey) }}
停止就像:
func stopRotatingView(view: UIView) { if view.layer.animationForKey(kRotationAnimationKey) != nil { view.layer.removeAnimationForKey(kRotationAnimationKey) }}
- 3 回答
- 0 關注
- 900 瀏覽
添加回答
舉報
0/150
提交
取消