1
我正在構建一個應用程序,我需要爲三個按鈕創建重力和旋轉。我爲我的一個按鈕編寫了一個無限旋轉函數,但我不知道如何將它鏈接到其他兩個按鈕。如何使多個對象旋轉?
import UIKit
class ViewController: UIViewController {
var animator: UIDynamicAnimator?
var gravity: UIGravityBehavior?
var isRotating = false
@IBOutlet var Ball1: UIButton!
@IBOutlet var Ball2: UIButton!
@IBOutlet var Ball3: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
self.animator = UIDynamicAnimator(referenceView: self.view)
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
Ball1.layer.addAnimation(spinAnimation, forKey: "transform.rotation.z")
} else {
// remove the animation
Ball1.layer.removeAllAnimations()
}
// toggle its state
isRotating = !isRotating
}
}
它的工作!謝謝KYE!很簡單! :) – Xcoder555