Swift Shake Gesture with Arrays

问题描述 投票:-3回答:1

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

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

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

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

swift xcode
1个回答
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
  }

}
© www.soinside.com 2019 - 2024. All rights reserved.