代码之家  ›  专栏  ›  技术社区  ›  VSB

swift:motionEnded不触发或实际设备

  •  0
  • VSB  · 技术社区  · 6 年前

    我在模拟器上运行我的骰子摇动应用程序。它确实触发了 motionEnded 做摇动手势。 不过,在真正的iphone 6上运行最新ios版本的示例源代码并没有帮助。 我从 this answer ,但由于我不知道objective-c语法,所以无法将所有行完全转换为swift。

    以下是我的一些代码:

    appdelegate.swift批准

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

    / / //viewcontroller.swift视图控制器 // / / /由Eve于19年2月23日创建。 /版权所有©2019 hypersaz。版权所有。 / /

    视图控制器.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()
        }
    
    }
    
    0 回复  |  直到 6 年前
        1
  •  0
  •   VSB    6 年前

    密码没问题!我真的需要摇动我的手机几次才触发 motionEnded .