Haptic FeedBack Still Going After Switching The View Controller

172 views Asked by At

I have a secondary View Controller that uses some haptic feedback. I am triggering the haptic feedback on a scheduled timer that loops every 14 seconds

Timer.scheduledTimer(withTimeInterval: 14, repeats: true) { _ in
                self.changeLabel()
                }

This timer calls a function that triggers 2 haptic feedback hits

@objc func changeLabel() {
        
    if counter2 == 1 {
       //Haptics
       let impactGenerator = UIImpactFeedbackGenerator(style: .medium)
       impactGenerator.prepare()
       impactGenerator.impactOccurred()
       Timer.scheduledTimer(withTimeInterval: 7, repeats: false) { _ in
       let impactGenerator = UIImpactFeedbackGenerator(style: .medium)
       impactGenerator.prepare()
       impactGenerator.impactOccurred()}
    //Changing Label
    self.mainFocusLabel.text = self.foclabel1Text
    self.manifestationImg.sd_setImage(with: URL(string: self.imglabel1URL))
    self.counter2 = 2

(theres more code but they are all almost identical to this block, the only differences are that it just changes the label to focuslabel2-10 and uses the counter to recognize what's the next label to change.

What is happening which I'm confused about is-- after the view is closed out-- the vibrations (and apparently the timer?) are still going. My phone is vibrating at the same intervals. I haven't seen any code to disable haptic feedback. I would imagine that I can just add that to the other view controller to solve this.

1

There are 1 answers

0
Rem Biths Wind On BEST ANSWER

Solved.. Kind of.

I had to make the timer a variable

var forteenSecTimer : Timer?

 forteenSecTimer = Timer.scheduledTimer(withTimeInterval: 14, repeats: true) { _ in
                self.changeLabel()
                }

And then to add this code to any exit or done buttons. I had to programmatically replace the back button so that I could make sure this code activated whenever the view was closed

forteenSecTimer?.invalidate()

I had to also change the view controller presentation style from a pop-over to just show because I wasnt sure how to activate that code when the view was just slid down to get rid of it.

Not a perfect solution but this is what I came up with.