
- iOS Tutorial
- iOS - Home
- iOS - Getting Started
- iOS - Environment Setup
- iOS - Objective-C Basics
- iOS - First iPhone Application
- iOS - Actions and Outlets
- iOS - Delegates
- iOS - UI Elements
- iOS - Accelerometer
- iOS - Universal Applications
- iOS - Camera Management
- iOS - Location Handling
- iOS - SQLite Database
- iOS - Sending Email
- iOS - Audio & Video
- iOS - File Handling
- iOS - Accessing Maps
- iOS - In-App Purchase
- iOS - iAd Integration
- iOS - GameKit
- iOS - Storyboards
- iOS - Auto Layouts
- iOS - Twitter & Facebook
- iOS - Memory Management
- iOS - Application Debugging
- iOS Useful Resources
- iOS - Quick Guide
- iOS - Useful Resources
- iOS - Discussion
How to make iPhone vibrate using Swift?
To make an iPhone vibrate using swift we’ll use two different methods. First create a new project and add Four different buttons to the main View controller.
Now import the AudioToolbox framework in your view controller class.
For the first button add an action and write the following code as shown below:
@IBAction func actionButtonOne(_ sender: Any) { AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate)) }
This will generate a long vibrating feedback on your device. Now to create more vibration effects on devices with iOS 10 or more we’ll add methods for all four different buttons.
@IBAction func actionButtonTwo(_ sender: Any) { let generator = UIImpactFeedbackGenerator(style: .heavy) generator.impactOccurred() } @IBAction func actionButtonThree(_ sender: Any) { let generator = UIImpactFeedbackGenerator(style: .light) generator.impactOccurred() } @IBAction func actionButtonFour(_ sender: Any) { let generator = UIImpactFeedbackGenerator(style: .medium) generator.impactOccurred() }
In the above three methods, we are using UIImpact Feedback generator to generate three different types of feedback. Now run the application on a device and feel the vibrations when you press these buttons. Sadly the output can not be shown in this example as it is a vibration feedback.
- Related Articles
- How to make an Android device vibrate programmatically using Kotlin?
- How to make an Android device vibrate?
- How to generate Unique ID of device for iPhone/iPad using Swift?
- How to make an Android device vibrate programmatically?
- How to determine device type (iPhone, iPod Touch) with Swift?
- How to make phone call in iOS 10 using Swift?
- How to make an HTTP request on iOS App using Swift?
- How to make the phone call in iOS 10 using Swift?
- How to make an HTTP POST request on iOS App using Swift?
- How to determine device type (iPhone, iPod Touch) with iPhone SDK?
- How to exit iPhone application gracefully?
- How to Find a Lost iPhone?
- How to detect shake gesture using Swift?
- How to resize an UIImageView using Swift?
- How to add constraints programmatically using Swift
