How to make my iphone vibrate twice when I press the button? IOS swift Xcode

I try to make the vibration twice as high as my iphone when I press the button (for example, the vibration of the sms signal)

With AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate)) I only get one normal vibration, but I need two shorts: /.

Can anybody help me? Thanks

+1
ios xcode swift
source share
3 answers
 #import <AudioToolbox/AudioServices.h> AudioServicesPlayAlertSound(UInt32(kSystemSoundID_Vibrate)) 

This is a quick function ... For a detailed description, see this article.

+4
source share

Update for iOS 10

With iOS 10, there are several new ways to do this with minimal code.

Method 1 - UIImpactFeedbackGenerator :

 let feedbackGenerator = UIImpactFeedbackGenerator(style: .heavy) feedbackGenerator.impactOccurred() 

Method 2 - UINotificationFeedbackGenerator :

 let feedbackGenerator = UINotificationFeedbackGenerator() feedbackGenerator.notificationOccurred(.error) 

Method 3 - UISelectionFeedbackGenerator :

 let feedbackGenerator = UISelectionFeedbackGenerator() feedbackGenerator.selectionChanged() 
+14
source share

Here is what I came up with:

 import UIKit import AudioToolbox class ViewController: UIViewController { var counter = 0 var timer : NSTimer? override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func vibratePhone() { counter++ switch counter { case 1, 2: AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) default: timer?.invalidate() } } @IBAction func vibrate(sender: UIButton) { counter = 0 timer = NSTimer.scheduledTimerWithTimeInterval(0.6, target: self, selector: "vibratePhone", userInfo: nil, repeats: true) } } 

When you press the button, the timer starts and repeats at the desired time interval. NSTimer calls the vibratePhone (Void) function, and from there I can control how many times the phone vibrates. In this case, I used the switch, but you could use if if else. Just set a counter to count each time the function is called.

+3
source share