ios - 检测摇动手势 IOS Swift

标签 ios iphone xcode swift gesture

我正在开发一个带有手势系统的应用程序,基本上,如果我将 iPhone 向左转动,我的应用程序将执行一个功能,如果我将 iPhone 向右转动,则使用其他手势执行其他功能。

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

有人有帮助我的教程吗?

最佳答案

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: UIEvent.EventSubtype, with event: UIEvent?) {
    if motion == .motionShake {
        print("Why are you shaking me?")
    }
}

关于ios - 检测摇动手势 IOS Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33503531/

相关文章:

ios - 如何在单个场景中为 SpriteKit 游戏中的事件发送信号?

ios - Swift 2.0 - 二元运算符 "|"不能应用于两个 UIUserNotificationType 操作数

javascript - 执行scrollTop时闪烁?

iphone - 带有 SDK 4.2 的 Xcode 始终启动 iPad 模拟器

ios - 尝试从 super View 中删除对象时 Objective-c 程序崩溃

xcode - 我应该在 SVN 中提交 .xcodeproj 文件吗?

ios - 为什么SFAuthenticationSession每次都提示同意?

iphone - UITabBarController 图像 iOS 5

iphone - 在 iPhone TableView 单元格的单元格右侧添加小箭头

ios - 应用程序因 SecTrustEvaluate [leaf IssuerCommonName SubjectCommonName] 错误而卡住