swift: motionEnded не запускает или реальное устройство - PullRequest
0 голосов
/ 25 февраля 2019

Я запускаю мое приложение для дрожания на Симуляторе.Это вызывает motionEnded, делая жест встряхивания.Однако запуск моего исходного кода на реальном iPhone 6 с последней версией iOS не помог.Я привел некоторые части из этого ответа , но так как я не знаю синтаксиса target-C, я не смог полностью перевести все строки в swift.

Вот некоторые фрагменты моего кода:

AppDelegate.swift

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

}

// // ViewController.swift // dicer // // Создано eve 23.02.19.// Copyright © 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()
    }

}

1 Ответ

0 голосов
/ 25 февраля 2019

Там не было ничего плохого с кодами!Мне нужно было несколько раз встряхнуть телефон, чтобы сработать motionEnded.

...