Swift 2.0 中的 motionEnded 方法
motionEnded method in Swift 2.0
我似乎无法使用 Swift 2.0/Xcode 7 的运动检测方法。我得到的错误指示:'Method does not override any method from its superclass'。我想使用内置的 UIEvent 方法检测各种动作......即摇动。这是我在 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
}
}
这里遇到了同样的问题:
这里的问题是 Swift 2.0 中的参数已更新。 UIEvent 参数现在是 UIEvent?。为了避免将来出现这些问题,如果您知道要覆盖的函数,只需开始输入 "override func ",Xcode 的自动完成功能应该会为您提供所需的功能。
只是想在 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
}
}
我似乎无法使用 Swift 2.0/Xcode 7 的运动检测方法。我得到的错误指示:'Method does not override any method from its superclass'。我想使用内置的 UIEvent 方法检测各种动作......即摇动。这是我在 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
}
}
这里遇到了同样的问题:
这里的问题是 Swift 2.0 中的参数已更新。 UIEvent 参数现在是 UIEvent?。为了避免将来出现这些问题,如果您知道要覆盖的函数,只需开始输入 "override func ",Xcode 的自动完成功能应该会为您提供所需的功能。
只是想在 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
}
}