Create a new Single View Application project in Xcode and modify ViewController.swift as:
import UIKit
import AudioToolbox
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//Buttons
let button1 : UIButton!
button1 = UIButton(frame: CGRectMake(0, 0, 300, 30))
button1.center = CGPointMake(view.center.x, 150)
button1.setTitle("Vibrate Alert", forState: UIControlState.Normal)
button1.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
button1.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Highlighted)
button1.addTarget(self, action: "btn1Pressed:", forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(button1)
let button2 : UIButton!
button2 = UIButton(frame: CGRectMake(0, 0, 300, 30))
button2.center = CGPointMake(view.center.x, 300)
button2.setTitle("Vibrate System", forState: UIControlState.Normal)
button2.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
button2.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Highlighted)
button2.addTarget(self, action: "btn2Pressed:", forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(button2)
}
func btn1Pressed(sender: UIButton) {
AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate), nil)
//AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
}
func btn2Pressed(sender: UIButton) {
AudioServicesPlaySystemSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate), nil)
//AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Result with an iPhone 5:
The iPhone vibrates when pressing either of the buttons.
Result with an iPad 3rd generation:
The iPad neither vibrates nor beeps when pressing either of the buttons.
References:
How to make iPhone vibrate using Swift?
Making the iPhone vibrate
No comments:
Post a Comment