检测摇动手势IOS Swift [英] Detect shake gesture IOS Swift

查看:153
本文介绍了检测摇动手势IOS Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个带有手势系统的应用程序,基本上如果我将iPhone转向左侧,我的应用程序将执行一项功能,如果我将iPhone转为Right,其他功能,以及其他手势。

I'm developing a app with a gesture system, basically if I turn the iPhone to left my app will do a function, if I turn the iPhone to Right, other function, with others gestures.

我不知道如何使用它,我正在尝试谷歌搜索但不工作,结果只是触摸手势而不是动作手势。

I don't have idea how to work with that, i'm trying search in google but not work, the result is only touch gesture and not motion gesture.

有人有教程可以帮助我吗?

someone have a tutorial to help me?

推荐答案

Swift3 ios10:

Swift3 ios10:

override func viewDidLoad() {
    super.viewDidLoad()
    self.becomeFirstResponder() // To get shake gesture
}

// We are willing to become first responder to get shake motion
override var canBecomeFirstResponder: Bool {
    get {
        return true
    }
}

// Enable detection of shake motion
override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
    if motion == .motionShake {
        print("Why are you shaking me?")
    }
}

这篇关于检测摇动手势IOS Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆