无限旋转UIView直到调用stop方法并将视图设置为其起始点的动画

时间:2014-06-18 21:56:56

标签: ios animation uiview uiviewanimation

我需要无限期地轮换UIView。但是,我需要能够按照方法停止它,因为我正在尝试实现类似自定义UIActivityIndicatorView的操作。视图还需要动画回到其起始旋转(0度)。到目前为止我一直试图实现的所有方法的问题是我要么无法手动停止它(直到持续时间结束),动画不顺畅或我的视图没有返回到它的开始位置。

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

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

2 个答案:

答案 0 :(得分:3)

好的,只需构建它以检查它是否可行。

在斯威夫特(因为我正在学习)我已经做到了......

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
    }
}

基本上,每次旋转都是一个动画。然后在完成块中,我检查一个布尔rotating。如果rotating == true那么我再次运行轮换。然后再次。再一次。

rotating == false时,我只是不再从完成块再次运行动画。

这可以确保在实际停止动画之前最后一个动画结束。

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

答案 1 :(得分:1)

将此代码添加到您想要执行动画的地方

在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;
    }
}

调用/启动动画

[self rotateAnimation:YES];

停止动画

[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")
        }
    }

调用/启动动画

rotateAnimation(true)

停止动画

rotateAnimation(false)