[英]Vibrate indefinitely in Swift
How can I make an iPhone vibrate indefinitely? 如何使iPhone无限期振动? I know I can do this: 我知道我可以这样做:
while true {
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
}
Is there any way to play the vibration for a certain number of seconds, or something with a similar result that is cleaner? 有什么办法可以使振动持续一定的秒数,或者有类似的结果更干净? Also, is there a way to make the vibration more intense? 另外,有没有办法使振动更剧烈?
I think you should use Timer . 我认为您应该使用Timer 。
var timer: Timer?
var numberRepeat = 0
var maxNumberRepeat = 20
func startTimer() {
//
timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(self.timerHandler), userInfo: nil, repeats: true)
}
func stopTimer() {
timer?.invalidate()
timer = nil
}
@objc func timerHandler() {
if numberRepeat <= maxNumberRepeat {
numberRepeat += 1
vibrateDevice()
} else {
stopTimer()
}
}
func vibrateDevice() {
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.