无限旋转UIView,直到调用停止方法,并将视图animation回到起始点

我需要无限期地旋转一个UIView 。 但是,我需要能够停止每一个方法,因为我试图实现像一个自定义的UIActivityIndicatorView 。 视图还需要animation回到其开始旋转(0度)。 到目前为止,我一直试图实现的所有方法的问题是,我无法手动停止它(直到持续时间结束),animation不平滑或我的视图不会返回到其开始位置。

基本上,我需要的是一个animation旋转我的看法永远。 一旦我调用一个方法来停止它,它应该返回到它的起点并停止。

我已经尝试了一些修改的答案,但没有成功。

好吧,只是build立这个检查是否会工作。

在斯威夫特(因为我正在学习),我做了这个…

 import UIKit class ViewController: UIViewController { @IBOutlet var rotatingView : UIView var rotating = false override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func start(sender : AnyObject) { rotating = true rotateOnce() } func rotateOnce() { UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveLinear, animations: {self.rotatingView.transform = CGAffineTransformRotate(self.rotatingView.transform, 3.1415926)}, completion: {finished in self.rotateAgain()}) } func rotateAgain() { UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveLinear, animations: {self.rotatingView.transform = CGAffineTransformRotate(self.rotatingView.transform, 3.1415926)}, completion: {finished in if self.rotating { self.rotateOnce() }}) } @IBAction func stop(sender : AnyObject) { rotating = false } } 

实质上,每一个旋转是一个animation。 然后在完成块我检查布尔rotating 。 如果rotating == true那么我再次运行旋转。 然后再次。 然后再次。

rotating == false我只是不要从完成块再次运行animation。

这可以确保在实际停止animation之前最后一个animation到达最终位置。

Objective-C版本

 #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UIView *rotatingView; @property (nonatomic, assign) BOOL rotating; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)startRotating:(id)sender { self.rotating = YES; [self firstRotation]; } - (IBAction)stopRotating:(id)sender { self.rotating = NO; } - (void)firstRotation { [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{ self.rotatingView.transform = CGAffineTransformRotate(self.rotatingView.transform, M_PI); } completion:^(BOOL finished) { [self secondRotation]; }]; } - (void)secondRotation { [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{ self.rotatingView.transform = CGAffineTransformRotate(self.rotatingView.transform, M_PI); } completion:^(BOOL finished) { if (self.rotating) { [self firstRotation]; } }]; } @end 

把这个代码添加到你想要执行animation的地方

在Objective-C中

 -(BOOL)rotateAnimation:(BOOL)check { if (check) { [UIView animateWithDuration:0.5f animations:^{ [ANIMATING_VIEW_OUTLET setAlpha:0.0f]; }completion:^(BOOL finished){ [ANIMATING_VIEW_OUTLET.layer removeAllAnimations]; [self.view sendSubviewToBack:ANIMATING_VIEW_OUTLET]; }]; return NO; } else { [self.view bringSubviewToFront:ANIMATING_VIEW_OUTLET]; [UIView animateWithDuration:0.5f animations:^{ [ANIMATING_VIEW_OUTLET setAlpha:1.0f]; }]; CABasicAnimation* animationFull = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; animationFull.fromValue = @0.0f; animationFull.toValue = @(2*M_PI); animationFull.duration = 0.75f; // this might be too fast animationFull.repeatCount = HUGE_VALF; // HUGE_VALF is defined in math.h so import it [ANIMATING_VIEW_OUTLET.layer addAnimation:animationFull forKey:@"rotation"]; return YES; } } 

调用/启动animation

 [self rotateAnimation:YES]; 

停止animation

 [self rotateAnimation:YES]; 

在Swift 2.2中

 func rotateAnimation(check : Bool) -> Bool { if check == true { UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: { kAnimatingViewOutlet.alpha = 0 }, completion: { (finished) in kAnimatingViewOutlet.layer.removeAllAnimations() }) return false } else if check == false { UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: { kAnimatingViewOutlet.alpha = 1 }, completion: { (finished) in }) let animationFull : CABasicAnimation = CABasicAnimation.init(keyPath: "transform.rotation.z") animationFull.fromValue = 0 animationFull.toValue = 2*M_PI animationFull.duration = 0.75 // this might be too fast animationFull.repeatCount = Float.infinity kAnimatingViewOutlet.layer.addAnimation(animationFull, forKey: "rotation") return true else { print("check value is nil") } } 

调用/启动animation

 rotateAnimation(true) 

停止animation

 rotateAnimation(false)