Description:
這demo是透過偵測手機搖動來更換圖片及震動手機。
iOS 在 UIResponder interface 中提供了三個有關 Motion detected funciton(motionBegan、motionEnded、motionCancelled) 來給開發者使用。此 demo 是在晃動手勢結束後更換圖片並震動裝置。
Component:
Highlight function:
let imgArray = ["01.jpg", "02.jpg", "03.jpg", "04.jpg", "05.jpg", "06.jpg"]
override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
updateImage()
}
func updateImage() {
var randIndex = Int(arc4random_uniform(6))
imgView.image = UIImage(named: imgArray[randIndex])
}
import AudioToolbox
override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
}
Reference:
Source code on Github