Search code examples
swiftswift2ios9superclassuievent

motionEnded method in Swift 2.0


I can't seem to get the motion detection methods to work with Swift 2.0/Xcode 7. The error I'm getting indicates: 'Method does not override any method from its superclass'. I want to detect various motions using the built in UIEvent methods.. i.e. shake. Here's what I have inside the class

    //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
        }
    }

The same problem was encountered here: Swift 2 migration problems


Solution

  • The issue here is that the parameters have been updated in Swift 2.0. The UIEvent parameter is now UIEvent?. To avoid these issues in the future if you know the function to be overridden just start typing in "override func " and Xcode's autocomplete should give you the desired function.