摇晃手机也是一种常用的交互手段(比如微信摇一摇功能)。iOS SDK中已经将shake事件方便地融合进去了,就像触发touch事件一样简单,发生摇晃事件后程序会自动执行。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func motionBegan(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
print("开始摇晃")
}
override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
print("摇晃结束")
}
override func motionCancelled(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
print("摇晃被意外终止")
}
}
网友评论