I'm running my dicer shake app on Simulator. It does trigger motionEnded
by doing shake gesture.
However running my sample source code on real iPhone 6 with latest iOS version, it did not help.
I've brought some parts from this answer, but since I don't know objective-C syntax I could not completely translate all lines to swift.
Here are some pieces of my code:
AppDelegate.swift
func applicationDidFinishLaunching(_ application: UIApplication) {
application.applicationSupportsShakeToEdit = true
}
// // ViewController.swift // dicer // // Created by eve on 2/23/19. // Copyright © 2019 Hypersaz. All rights reserved. //
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()
}
}