Swift 2.0中的motionEnded方法

时间:2015-10-05 04:32:40

标签: swift swift2 ios9 superclass uievent

我似乎无法让运动检测方法与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 problems

2 个答案:

答案 0 :(得分:4)

这里的问题是参数已在Swift 2.0中更新。 UIEvent参数现在是UIEvent?。如果您知道要覆盖的功能,请在将来开始输入"覆盖func"以避免将来出现这些问题。和Xcode的自动完成应该给你所需的功能。

答案 1 :(得分: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
    }
}