3

我似乎无法让运动检测方法与 Swift 2.0/Xcode 7 一起使用。我得到的错误表明:“方法不会覆盖其超类中的任何方法”。我想使用内置的 UIEvent 方法检测各种运动。即摇晃。这是我在课堂上的内容

    //this is used for detecting UIEvents i.e. 'Shake'
    override func canBecomeFirstResponder() -> Bool {
    return true
    }

……

    override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent)
    {
        if motion == .MotionShake && randomNumber == SHAKE
        {
            print("SHAKE RECEIVED")
            correctActionPerformed = true
        }
        else if motion == .MotionShake && randomNumber != SHAKE
        {
            print("WRONG ACTION")
            wrongActionPerformed = true
        }
         else
        {
            print("WRONG ACTION")
            wrongActionPerformed = true
        }
    }

这里遇到了同样的问题:Swift 2 migration questions

4

2 回答 2

4

这里的问题是参数已在 Swift 2.0 中更新。UIEvent 参数现在是 UIEvent?。如果您知道要覆盖的函数,为避免将来出现这些问题,只需开始输入“override func”,Xcode 的自动完成功能应该会为您提供所需的功能。

于 2015-10-05T14:54:37.760 回答
4

只是想在 Swift 3 iOS 10 中添加相同的内容:

override var canBecomeFirstResponder: Bool {
    return true
}

override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
    if motion == .motionShake && randomNumber == SHAKE
    {
        debugPrint("SHAKE RECEIVED")
        correctActionPerformed = true
    }
    else if motion == .motionShake && randomNumber != SHAKE
    {
        debugPrint("WRONG ACTION")
        wrongActionPerformed = true
    }
    else
    {
        debugPrint("WRONG ACTION")
        wrongActionPerformed = true
    }
}
于 2016-10-01T15:22:16.417 回答