Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我如何从我的swift应用程序中处理对ios设备震动的支持?_Ios_Swift_Uievent - Fatal编程技术网

我如何从我的swift应用程序中处理对ios设备震动的支持?

我如何从我的swift应用程序中处理对ios设备震动的支持?,ios,swift,uievent,Ios,Swift,Uievent,我正在编写一个ios swift应用程序,我想包括一个对摇动设备的支持。至于现在,我想打印一个消息到控制台时,用户摇他的手机。我找到了这个教程,看起来非常简单,但是有一件事困扰着我 我希望它可以从应用程序中的任何视图工作,而不仅仅是单个视图。所以,无论用户当前在应用程序中的哪个位置,他都应该能够调用shake方法。我应该在每个面板中实现方法override func motionEnded(motion:UIEventSubtype,withEvent-event:UIEvent){,还是有办法

我正在编写一个ios swift应用程序,我想包括一个对摇动设备的支持。至于现在,我想打印一个消息到控制台时,用户摇他的手机。我找到了这个教程,看起来非常简单,但是有一件事困扰着我


我希望它可以从应用程序中的任何视图工作,而不仅仅是单个视图。所以,无论用户当前在应用程序中的哪个位置,他都应该能够调用shake方法。我应该在每个面板中实现方法
override func motionEnded(motion:UIEventSubtype,withEvent-event:UIEvent){
,还是有办法实现一次并在整个应用程序中填充它?

首先让我们看看这些“motion”方法的来源,如文档所述:

UIResponder类为响应和处理事件的对象定义了一个接口。它是UIApplication、UIView及其子类的超类。()

运动事件的事件处理方法有:

func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent?)
func motionCancelled(motion: UIEventSubtype, withEvent event: UIEvent?)
func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?)
因此,正如您所看到的,要在应用程序的每个屏幕上“捕捉”运动事件,我们应该在这些屏幕上覆盖这些方法。感谢上帝,通过扩展,我们可以让它变得更容易:)

要包含“运动”逻辑,请创建一个协议并将其命名为“MotionDelegate”:

protocol MotionDelegate {
func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent?)
func motionCancelled(motion: UIEventSubtype, withEvent event: UIEvent?)
func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?)
}

并对UIViewController进行扩展,符合MotionDelegate协议:

extension UIViewController:MotionDelegate {
override public func becomeFirstResponder() -> Bool {
    return true
}

override public func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent?) {
    if motion == .MotionShake { print("Shaking motionBegan with event\(event)") }
}

override public func motionCancelled(motion: UIEventSubtype, withEvent event: UIEvent?) {
    if motion == .MotionShake { print("Shaking motionCancelled with event\(event)") }
}

override public func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) {
    if motion == .MotionShake { print("Shaking motionEnded with event\(event)") }
}
}

这样,运动处理将在应用程序的每个UIViewController实例上工作

要在某些vc上处理运动事件,您应该在其扩展中覆盖它:

extension MotionViewController {
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) {
    if motion == .MotionShake {
        print("MotionViewController Shaking motionEnded with event\(event)")
    }
}
}


希望有帮助

谢谢你,这确实很有帮助!只有两个小问题-我应该把这个协议放在一个全新的单独的文件中吗?扩展也是一样——我可以把它放在哪里呢?这其实并不重要,但这是一种将文件划分为逻辑部分的好方法,所以我建议在新文件中实现协议和vc扩展。看起来不错,在运行ion iOS 10.0.2的swift 3.0下编译,但遗憾的是什么都没有,绝对没有。