識別搖晃手勢

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var shakingLb: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    override func motionBegan(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
        self.shakingLb.isHidden = false
    }
    
    override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
        DispatchQueue.main.asyncAfter(deadline:.now() + 1.0) {
            self.shakingLb.isHidden = true
        }
    }
    
    override func becomeFirstResponder() -> Bool {
        return true
    }

}

Demo 11鏈接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章