我正在模拟器上运行我的切丁机摇应用程序。它确实通过摇动手势来触发motionEnded
。
但是,在带有最新iOS版本的真实iPhone 6上运行我的示例源代码,这没有帮助。
我带来了this answer的一些内容,但是由于我不了解Objective-C语法,所以无法完全将所有行转换为swift。
以下是我的一些代码:
AppDelegate.swift
func applicationDidFinishLaunching(_ application: UIApplication) {
application.applicationSupportsShakeToEdit = true
}
// // ViewController.swift //切块机 // //由前夕在19/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()
}
}
答案 0 :(得分:0)
代码没有错!我确实几乎不需要摇晃手机几次即可触发using (var session = OpenSession())
{
}
。