我在模拟器上运行我的dicer摇动应用程序。它确实通过做摇动手势触发motionEnded
。然而,在最新的iOS版本的真实iPhone 6上运行我的示例源代码,它没有帮助。我从this answer带来了一些部分,但由于我不知道Objective-C语法,所以我无法将所有行完全转换为swift。
以下是我的一些代码:
AppDelegate.swift
func applicationDidFinishLaunching(_ application: UIApplication) {
application.applicationSupportsShakeToEdit = true
}
// // ViewController.swift // dicer // //由eve于2/23/19创建。 //版权所有©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
。