我对 Swift 和编程非常陌生。我正在尝试创建由 UILongPressGestureRecognizer 触发的触觉反馈模式。当用户“长按”屏幕时,我希望手机振动 3 次,每次振动之间有 1 秒的延迟。我尝试使用“睡眠”来完成 1 秒的延迟,但这不起作用。正确执行此操作的最佳方法是什么?
var feedbackGenerator : UIImpactFeedbackGenerator? = nil
func performFeedbackPattern() {
//create the feedback generator
feedbackGenerator = UIImpactFeedbackGenerator(style: .heavy)
feedbackGenerator?.prepare()
//play the feedback three times with 1 second between each feedback
feedbackGenerator?.impactOccurred()
sleep (1)
feedbackGenerator?.impactOccurred()
sleep (1)
feedbackGenerator?.impactOccurred()
}
@IBAction func gestureRecognizer(_ sender: UILongPressGestureRecognizer) {
switch sender.state {
case .began:
performFeedbackPattern()
default: break
}