Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you make device vibrate at different frequencies using swift?

Tags:

ios

swift

iphone

I want to make an app that make phone vibrate at different frequencies. How can I control the frequency of the phone vibrating?

like image 468
Leo Avatar asked Nov 06 '16 14:11

Leo


1 Answers

I don't know if it helps, but in my case I wanted to have longer vibration for some reasons & I created this extension for it.

extension UIViewController {

    func vibrate(_ style: UIImpactFeedbackGenerator.FeedbackStyle = .heavy) {
        let impactFeedbackgenerator = UIImpactFeedbackGenerator(style: style)
        impactFeedbackgenerator.prepare()
        impactFeedbackgenerator.impactOccurred()
    }

    func vibrateBomb() {
        for i in 0...4 {
            DispatchQueue.main.asyncAfter(deadline: .now() + Double(i * 1)/3) {
                self.vibrate()
            }
            DispatchQueue.main.asyncAfter(deadline: .now() + Double(i * 1)/5) {
                self.vibrate()
            }
            DispatchQueue.main.asyncAfter(deadline: .now() + Double(i * 1)/8) {
                self.vibrate()
            }
        }
    }
 }
like image 160
Ahmadreza Avatar answered Nov 07 '22 06:11

Ahmadreza