gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:59:03 36 4
gpt4 key购买 nike

我正在模拟器上运行我的 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/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com