swift 中的 Motion Shake 动作不再有效

Motion shake action not working anymore in swift

您好,我遇到了一些困难,我一直在为我的应用程序重新设计菜单导航。我设法做到了。但现在我的应用程序的一项功能已决定停止运行。

想法是你摇动你的 phone 它会在 运行dom 中选择一张图片,与应用程序分开的代码工作正常,就像它在所有以前的版本上所做的一样,我什至 运行 它以现在的形式很快就以它自己的形式存在,以防万一而且它工作得很好。

希望有人能指出我出错的方向。

我的代码如下;

    import Foundation
import UIKit


class cocktailChoice: UIViewController {

    @IBOutlet weak var drinkImage: UIImageView!

    var drinkNamesArray:[String] = ["cocktailList0","cocktailList1","cocktailList2","cocktailList3","cocktailList4","cocktailList5","cocktailList6","cocktailList7","cocktailList8","cocktailList9","cocktailList10","cocktailList11","cocktailList12","cocktailList13","cocktailList14","cocktailList15","cocktailList16","cocktailList17","cocktailList18","cocktailList19","cocktailList20","cocktailList21","cocktailList22","cocktailList23","cocktailList24","cocktailList25","cocktailList26","cocktailList27","cocktailList28","cocktailList29","cocktailList30","cocktailList31"]

    override func viewDidLoad() {
        self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    }

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

        if motion == .motionShake{

            let firstRandomNumber = Int(arc4random_uniform(32))

            let DrinkString:String = self.drinkNamesArray[firstRandomNumber]

            self.drinkImage.image = UIImage(named: DrinkString)

        }

    }

}

编译没有错误,IBOutlet 已连接,没有我能看到的错误,但摇动动作不想播放。现在变得非常沮丧。

代替这部分 if motion == .motionShake{ ,你可以尝试使用这个:

if(event.subtype == .motionShake) {
        print("Shake event!")
    }