打開 ViewController.swift 文件,首先要讓 View Controller 回應(yīng)點(diǎn)擊事件,可以通過 ViewController FirstResponder 實(shí)現(xiàn),添加下列方法:
override func becomeFirstResponder() -> Bool {
return true
}
接下來,要想檢測(cè)搖一搖手勢(shì),添加 motionEnded(_:with:) 方法。
override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
if motion == .motionShake {
shakeLabel.text = "Shaken, not stirred"
}
}