Quantcast
Channel: Creating a haptic feedback pattern in swift 3 - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Creating a haptic feedback pattern in swift 3

$
0
0

I am very new to Swift and programming. I'm trying to create a pattern of haptic feedback triggered by a UILongPressGestureRecognizer. When the user "long presses" the screen, I want the phone to vibrate three times with a 1 second delay between each vibration. I tried using "sleep" to accomplish the 1 second delays, but this didn't work. What is the best way to do this correctly?

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
    }

Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>