2015-06-16 129 views
0

我發現這個代碼可以讓你在Swift中按下按鈕時旋轉/旋轉90度。但是我想要做的是在按下按鈕時無限旋轉/旋轉按鈕,並在再次按下按鈕時停止旋轉。這裏是我的代碼有:無限旋轉按鈕:Swift

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) 

    })) 
} 

} 

回答

5

可以使用CABasicAnimation無限動畫播放如下:

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 
    } 

} 
+0

我試過這個,但是我收到一條錯誤消息,說CABasicsAnimation沒有名爲'layer'的成員。我在這部分代碼中得到了這個消息:spinButton.layer.addAnimation(spinAnimation,forked:「transform.rotation.z」) –

+0

spinButton.layer應該是一個不是CABasicAnimation的按鈕,因爲你的錯誤狀態 –

+0

我想我只是不'對此我非常瞭解,所以我不確定如何解決這個問題。這是非常新的。我知道一些JavaScript的基礎知識,但這不是很多 –

0

它應該是一樣的你。

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 
    } 

} 
+1

spinAnimation.layer.addAnimation(spinAnimation,forKey:「transform.rotation.z」)更改爲spinButton.layer.addAnimation(spinAnimation,forKey:「transform.rotation.z」) –

+0

將M_1_PI更改爲M_PI –

+1

不錯!這工作。非常感謝你的幫助!非常酷。 –