简体   繁体   中英

Spin button infinitely: Swift

I found this code that allows you to rotate/spin 90 degrees when button is pushed in Swift. But what I would like to do is rotate/spin the button infinitely when it is pushed, and stop spinning when the button is pushed again. Here is the code I have:

import UIKit

class ViewController: UIViewController {

  @IBOutlet var otherbutton: UIButton!
  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 Rotate(sender: AnyObject) {

  UIView.animateWithDuration(1.0,
  animations: ({

    self.otherbutton.transform = CGAffineTransformMakeRotation(90)

  }))
}

}

You can use a CABasicAnimation to animate it infinitely as follow:

class ViewController: UIViewController {

    @IBOutlet weak var spinButton: UIButton!

    // create a bool var to know if it is rotating or not
    var isRotating = false

    @IBAction func spinAction(sender: AnyObject) {
        // check if it is not rotating
        if !isRotating {
            // create a spin animation
            let spinAnimation = CABasicAnimation()
            // starts from 0
            spinAnimation.fromValue = 0
            // goes to 360 ( 2 * π )
            spinAnimation.toValue = M_PI*2
            // define how long it will take to complete a 360
            spinAnimation.duration = 1
            // make it spin infinitely
            spinAnimation.repeatCount = Float.infinity
            // do not remove when completed
            spinAnimation.removedOnCompletion = false
            // specify the fill mode
            spinAnimation.fillMode = kCAFillModeForwards
            // and the animation acceleration
            spinAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
            // add the animation to the button layer
            spinButton.layer.addAnimation(spinAnimation, forKey: "transform.rotation.z")

        } else { 
            // remove the animation
            spinButton.layer.removeAllAnimations()
        }
        // toggle its state
        isRotating = !isRotating
    }

}

It should be the same as yours.

import UIKit

class ViewController: UIViewController {


    @IBOutlet weak var spinButtton: UIButton!

    var isRotating = 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 spinAction(sender: AnyObject) {

        if !isRotating {
            let spinAnimation = CABasicAnimation()
            // start from 0
            spinAnimation.fromValue = 0
            // goes to 360
            spinAnimation.toValue = M_1_PI * 2
            // define how long it will take to complete a 360
            spinAnimation.duration = 1
            // make spin infinitely
            spinAnimation.repeatCount = Float.infinity
            // do not remove when completed
            spinAnimation.removedOnCompletion = false
            // specify the fill mode
            spinAnimation.fillMode = kCAFillModeForwards
            // animation acceleration
            spinAnimation.timingFunction = CAMediaTimingFunction (name: kCAMediaTimingFunctionLinear)
            // add the animation to the button layer
            spinAnimation.layer.addAnimation(spinAnimation, forKey: "transform.rotation.z")

        } else {
            // remove the animation
            spinButtton.layer.removeAllAnimations()
        }

        // toggle  its state
        isRotating = !isRotating
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM