Swift Shake Gesture with Arrays

时间:2018-03-02 03:00:09

标签: swift xcode

如何使用设置为数组的随机生成器来使用摇动手势?是否可以使用阵列进行摇动手势?

override func becomeFirstResponder() -> Bool {
    return true
}

override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
    if motion == .motionShake {
    }
}

@IBAction func randomActivity(_ sender: Any) {
    let array = [

image

1 个答案:

答案 0 :(得分:0)

是的,这是可能的。您已正确设置了部分代码,但这是您可以采取的措施来解决您的问题(将来发布您的代码会更好,而不是截屏^ ____ *):

class ViewController: UIViewController {


var activityArray = ["Go For A Walk", "Go For A Jog", "Get Some Coffee", "Debug"]


override func viewDidLoad() { super.viewDidLoad()

  //1. Register As 1st Responders
  self.becomeFirstResponder()

}

override func motionBegan(_ motion: UIEventSubtype, with event: UIEvent?) {

    //1. Generate A Random Number Between 0 & The Acitivy Array Count
    let randomIndex = RandomInt(min: 0, max: activityArray.count-1)
    print("Lets \(activityArray[randomIndex])")
}


/// Generater A Random Int Between Two Numbers
///
/// - Parameters:
///   - min: Int
///   - max: Int
/// - Returns: Int
func RandomInt(min: Int, max: Int) -> Int {
    if max < min { return min }
    return Int(arc4random_uniform(UInt32((max - min) + 1))) + min
  }

}