ios - swift:motionEnded 不触发或真实设备

标签 ios swift gesture motionevent

我正在模拟器上运行我的 dicer shake 应用程序。它确实通过摇动手势触发 motionEnded。 然而,在具有最新 iOS 版本的真实 iPhone 6 上运行我的示例源代码,它没有帮助。 我从 this answer 带来了一些零件,但由于我不知道 Objective-C 语法,我无法将所有行完全转换为 swift。

这是我的一些代码:

AppDelegate.swift

func applicationDidFinishLaunching(_ application: UIApplication) {
    application.applicationSupportsShakeToEdit = true

}

// //ViewController.swift //切 block 机 // //由 eve 在 2019 年 2 月 23 日创建。 //版权所有 © 2019 Hypersaz。版权所有。 //

ViewController.swift

import UIKit

class ViewController: UIViewController {
    var randomDiceIndex1:Int = 0
    var randomDiceIndex2:Int = 0
    let diceArray = ["dice1","dice2","dice3","dice4","dice5","dice6"]

    @IBOutlet weak var diceImageView1: UIImageView!
    @IBOutlet weak var diceImageView2: UIImageView!



    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        updateDiceImages()
    }
    @IBAction func rollButtonPressed(_ sender: UIButton) {
         // A Block of Code
        updateDiceImages()

    }
    func updateDiceImages(){
        randomDiceIndex1 = Int.random(in: 0 ... 5)
        randomDiceIndex2 = Int.random(in: 0 ... 5)
        print(randomDiceIndex1)
        diceImageView1.image = UIImage(named: diceArray[randomDiceIndex1])
        diceImageView2.image = UIImage(named: diceArray[randomDiceIndex2])

    }
    override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
            updateDiceImages()
    }

    override var canBecomeFirstResponder: Bool{
        return true
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        becomeFirstResponder()
    }

}

最佳答案

代码没有任何问题!我确实需要摇晃手机几次才能触发 motionEnded

关于ios - swift:motionEnded 不触发或真实设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54862522/

相关文章:

ios - Xcode:启动屏幕未经许可改变颜色

ios - DateFormatter 不返回 "HH:mm:ss"的日期

ios - 以编程方式约束 UITextField 会导致错误 "Unable to activate constraint because they have no common ancestor."

Android ViewFlipper + 手势检测器

ios - 如何在 ios 中使用运行 ffmpeg 命令

ios - Strongloop环回ios swift sdk覆盖存储库错误

ios - 自定义顶部导航栏

uitableview - 自动布局 : Get UIImageView height to calculate cell height correctly

ios - Swift - Textview 识别点击的单词不起作用

java - Android Studio Google Glass 模拟器中未检测到点击/滑动