HapticFeedback
HapticFeedback is easy to use iOS haptic feedback generator. Besides default haptic feedbacks it can play patterns! Checkout 'ExampleHapticFeedback' project.
Example project
To run the example project, clone the repo, and run pod install
from the Example directory first.
Usage
To use a HapticFeedback, all you will need to import 'HapticFeedback' module into your swift file:
import HapticFeedback
To use an instance of a HapticFeedback, you just need to implement the extension with the Hapticable
protocol:
// MARK: Hapticable
extension YourClass: Hapticable {
/// Now you can use haptic feedback instance
private func someMethod() {
hapticFeedback.generate(.heavy)
}
}
Or you can instantiate 'HapticFeedback' instance manually:
/// HapticFeedback instance
let hapticFeedback = HapticFeedback()
Keep in mind that depending on the device you are using - the feedback might be different. Starting from iPhone 7 all the iPhones are coming with haptic engine. Prior to iPhone 7 HapticFeedback will use standard vibration. Also Pattern play and impacts are not supported on devices prior to iPhone 7 and iOS 10
1. Generating predefined default feedbacks.
With HapticFeedback you can generate three default types of haptic notifications:
// MARK: - HapticFeedbackNotification
public enum HapticFeedbackNotification {
case success
case warning
case error
}
Here is small example how can you generate Success, Warning and Error feedbacks:
/// Generate success feedback
hapticFeedback.generate(.success)
/// Generate warning feedback
hapticFeedback.generate(.warning)
/// Generate error feedback
hapticFeedback.generate(.error)
2. Generating impact feedbacks
Besides default haptic notifications, HapticFeedback can also generate impact notifications. The library supports three types of impact notifications (available in iOS10+ and staring from iPhone 7):
// MARK: - HapticFeedbackImpactStyle
public enum HapticFeedbackImpactStyle {
case light
case medium
case heavy
}
And use their call, similar to the previous example:
/// Generate medium feedback
hapticFeedback.generate(.medium)
3. Pattern play
And now something special - Pattern Play. HapticFeedback can play patterns, "coded" as String. Let's take a look at small example:
/// Generate custom pattern
hapticFeedback.generate("..oooO-O-Oooo..", delay: 0.1)
Each character in this string represents some specific haptic impact:
- "O" - heavy impact
- "o" - medium impact
- "." - light impact
- "-" - delay which has duration of 0.1 second
4. UI extensions
HapticFeedback comes with a couple of UI Extensions: HapticFeedbackButton
(which is subclass of UIButton
) and UIViewController
extension with Hapticable
protocol.
In order to start using HapticFeedbackButton
- just set the class name in your project to HapticFeedbackButton
. Then with UIControlEvents.touchDown
event you will get heavy impact feedback and with UIControlEvents.touchUpInside
and UIControlEvents.touchUpOutside
you will get light impact feedback.
UIViewController extension:
/// Present view controller with haptic feedback notifications
/// - Parameters:
/// - viewControllerToPresent: current view controller
/// - flag: bool flag
/// - hapticFeedbackNotification: hapticFeedbackNotification types
/// - completion: completion handler
public func present(
_ viewControllerToPresent: UIViewController,
animated flag: Bool,
hapticFeedbackNotification: HapticFeedbackNotification,
completion: (() -> Void)? = nil
) {
present(viewControllerToPresent, animated: flag, completion: completion)
hapticFeedback.generate(hapticFeedbackNotification)
}
To use HapticFeedback together with UIViewtController
extension follow this simple example:
/// Show notification alert
func showAlert(title: String, message: String, hapticFeedbackNotification: HapticFeedbackNotification) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default))
/// Here like this
present(alert, animated: true, hapticNotification: hapticFeedbackNotification)
}
Requirements
- iOS 10.0+
- Xcode 9.0
- Swift 5
Communication
- If you found a bug, open an issue.
- If you have a feature request, open an issue.
- If you want to contribute, submit a pull request.
Installation
Cocoapods
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapods
To install it using CocoaPods, simply add the following line to your Podfile:
use_frameworks!
target "<Your Target Name>" do
pod "haptic-feedback", :git => "https://github.com/Incetro/haptic-feedback", :tag => "[1.0.3]"
end
Then, run the following command:
$ pod install
Manually
If you prefer not to use any dependency managers, you can integrate Hapticfeedback into your project manually.
Embedded Framework
-
Open up Terminal,
cd
into your top-level project directory, and run the following command "if" your project is not initialized as a git repository:$ git init
-
Add HapticFeedback as a git submodule by running the following command:
$ git submodule add https://github.com/incetro/haptic-feedback.git
-
Open the new
haptic-feedback
folder, and drag theHapticFeedback.xcodeproj
into the Project Navigator of your application's Xcode project.It should appear nested underneath your application's blue project icon. Whether it is above or below all the other Xcode groups does not matter.
-
Select the
HapticFeedback.xcodeproj
in the Project Navigator and verify the deployment target matches that of your application target. -
Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.
-
In the tab bar at the top of that window, open the "General" panel.
-
Click on the
+
button under the "Embedded Binaries" section. -
You will see two different
HapticFeedback.xcodeproj
folders each with two different versions of theHapticFeedback.framework
nested inside aProducts
folder. -
Select the
HapticFeedback.framework
.You can verify which one you selected by inspecting the build log for your project. The build target for
Nio
will be listed as eitherHapticFeedback iOS
. -
And that's it!
The
HapticFeedback.framework
is automagically added as a target dependency, linked framework and embedded framework in a copy files build phase which is all you need to build on the simulator and a device.
Authors
Gasol: [email protected], incetro: [email protected]
License
HapticFeedback is available under the MIT license. See the LICENSE file for more info.