class: Shake
摇一摇
func
setSupportsShakeToEdit(_ edit:Bool)
开启\关闭 摇一摇功能
shakeBegan(_ motion: UIEventSubtype, with event: UIEvent?)
检测到摇动开始
shakeEnded(_ motion: UIEventSubtype, with event: UIEvent)
摇动结束
playAlertSoundVibrate()
手机振动
使用注意
此类为
ViewController
的扩展ViewController
中重写motionEnded(_ motion: UIEventSubtype, with event: UIEvent?)
会导致对应的shakeBegan(_ motion: UIEventSubtype, with event: UIEvent?)
失效,需自己重新实现ViewController
中重写motionBegan(_ motion: UIEventSubtype, with event: UIEvent?)
会导致对应的shakeEnded(_ motion: UIEventSubtype, with event: UIEvent)
失效,需自己重新实现
使用
-- class ViewController
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// 关闭功能
setSupportsShakeToEdit(false)
}
override func viewDidLoad() {
super.viewDidLoad()
// 如果shakeBegan未响应,可使用此方法
setSupportsShakeToEdit(true)
}
// MARK: - ShakeToEdit 摇动手机之后的回调方法
override func shakeBegan(_ motion: UIEventSubtype, with event: UIEvent?) {
// your code
debugPrint("检测到摇动开始")
}
override func shakeEnded(_ motion: UIEventSubtype, with event: UIEvent) {
// your code
playAlertSoundVibrate()
debugPrint("摇动结束")
}