简体   繁体   English

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

[英]How can I handle support for shaking the ios device from my swift app?

I'm writing an ios swift app and I want to include a support for shaking the device. 我正在编写一个ios swift应用程序,我想要包括摇动设备的支持。 As for now I want to print a msg to the console when user shakes his phone. 至于现在我想在用户摇动手机时将一个msg打印到控制台。 I found this tutorial http://www.ioscreator.com/tutorials/detect-shake-gesture-ios8-swift and it looks super easy, however there's one thing that bothers me. 我找到了这个教程http://www.ioscreator.com/tutorials/detect-shake-gesture-ios8-swift ,它看起来非常简单,但是有一件事困扰着我。

I want it to work from any view in the app, not only a single one. 我希望它可以在应用程序的任何视图中工作,而不仅仅是单个视图。 So no matter where user currently is in the app - he should be able to invoke the shake method. 因此,无论用户当前在应用程序中的哪个位置 - 他都应该能够调用摇动方法。 Should I implement the method override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) { in every panel then? 我应该在每个面板中实现方法override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) { Or is there a way of implementing it once and populating it across the whole app? 或者有没有一种方法可以实现它并在整个应用程序中填充它?

First of all lets find out where these 'motion' methods come from, as docs say: 首先,我们可以找出这些“运动”方法的来源,正如文档所说:

The UIResponder class defines an interface for objects that respond to and handle events. UIResponder类为响应和处理事件的对象定义接口。 It is the superclass of UIApplication, UIView and its subclasses.. ( https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIResponder_Class/ ) 它是UIApplication,UIView及其子类的超类。( https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIResponder_Class/

The event-handling methods for motion events are: 运动事件的事件处理方法是:

func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent?)
func motionCancelled(motion: UIEventSubtype, withEvent event: UIEvent?)
func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?)

So, as you see, to 'catch' motion events on every screen of the app - we should override these methods in these screens. 因此,如您所见,要在应用的每个屏幕上“捕捉”运动事件 - 我们应该在这些屏幕中覆盖这些方法。 Thanks God, with extensions - we can make it much easier :) 感谢上帝,有了扩展 - 我们可以让它变得更容易:)

To incapculate 'motion' logic lets make a protocol and name it 'MotionDelegate': 为了使'运动'逻辑更复杂,我们制定一个协议并命名为'MotionDelegate':

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

} }

And make an extension of UIViewController, conforming MotionDelegate protocol: 并制作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)") }
}

} }

In this way motion handling will be working on EVERY UIViewController instances of your appliction. 通过这种方式,运动处理将在您的应用程序的每个UIViewController实例上运行。

To handle motion event on some certain vc your should override it in its extension: 要处理某些特定vc上的动作事件,您应该在其扩展名中覆盖它:

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

} }

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Xcode 9的iOS设备上运行我的App? - How I can run my App on my iOS device in Xcode 9 ? Swift onesignal可以让我的应用程序支持ios 9和ios 10通知 - Swift onesignal can I allow my app to support both ios 9 and ios 10 notifications 如何在我的应用中决定支持最旧的iOS版本? - How can I decide the oldest iOS version to support in my app? 如何使我的iOS应用处理特定域? - How can I make my iOS app handle a specific domain? 如何在未越狱的iOS设备上测试已签名的应用程序 - How can I test my signed app on an unjailbroken iOS device 如何在我的iOS应用中播放广播? Swift和AVFoundation - How can I stream radio in my iOS app? Swift and AVFoundation Swift,Xcode,iOS-如何处理通过“在...中打开”从另一个应用程序发送到我自己的应用程序的文件 - Swift , Xcode, iOS - How to handle a file sent with 'Open in…' from another app to my own app 如何使用 swift 从我的 ios 应用程序通过 WhatsApp 向特定联系人发送消息? - How can I send message to specific contact through WhatsApp from my ios app using swift? 如何从NSData上保存iOS设备上的图像? - How can I save an image on my iOS device from NSData? 如何检测iOS设备无法播放我的音频流URL-swift3 - How can I detect if my audio streaming url can not be play by iOS device - swift3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM