【发布时间】:2021-03-10 19:44:28
【问题描述】:
我在 xcode 中制作了一个通过按钮振动的应用程序,但我想在振动期间添加一定持续时间的间隙。我正在使用 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate),我想在振动发生时添加间隙。然后添加哔声 3 秒并使用循环重复此操作。这就是我到目前为止所拥有的。如何创建循环?我正在使用 iphone 7 plus 进行测试。
class ViewController: UIViewController {
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let sound = Bundle.main.path(forResource: "beep", ofType: "mp3")
do{
// initialized it with URL created above
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: sound!))
}
catch{
print(error)
}
}
@IBAction func PressMe(_ sender: Any) {
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
for _ in 1...6 {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
Thread.sleep(forTimeInterval: 0.002)
let seconds = 1.0
DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
self.audioPlayer.play()
}
}
}
}
【问题讨论】:
标签: swift iphone xcode xcode12