Taptic in iOS 9

2019-01-31 04:23发布

问题:

Can you use the taptic engine in iOS 9 with iPhone 6s? WatchOS2 and OS X have the ability to use the haptic engine, so I assumed it would be in iOS 9 too, but I coudn't find any APIs for it.

回答1:

Yay, I had reverse engineered internal UIKit stuff and I found another (much simpler) way to actuate feedback via TapticEngine! We can just use AudioToolbox framework and several magic constants.

import AudioToolbox

AudioServicesPlaySystemSound(1519) // Actuate `Peek` feedback (weak boom)
AudioServicesPlaySystemSound(1520) // Actuate `Pop` feedback (strong boom)
AudioServicesPlaySystemSound(1521) // Actuate `Nope` feedback (series of three weak booms)

Hope this helps!



回答2:

There is currently no public available API in iOS 9 and iOS 9.1.

Disclaimer: There is a way to interact with Taptic Engine directly, but there is a private method. You should not use it in App Store applications.

However, if you are more into experimenting, then you can find that there is a new private class available in iOS 9: _UITapticEngine. You can find it's header here. To get to it, there is a new property on UIDevice class, called _tapticEngine. See the full header for UIDevice here. You can go ahead and import those headers, or just use NSSelectorFromString function and performSelector: method to get to the taptic engine:

id tapticEngine = [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"_tapticEngine") withObject:nil];
[tapticEngine performSelector:NSSelectorFromString(@"actuateFeedback:") withObject:@(1001)]; // Peek
[tapticEngine performSelector:NSSelectorFromString(@"endUsingFeedback:") withObject:@(1002)]; // Pop

This will activate the taptic engine for specific gesture, although both Peek and Pop feel similar to me. If you specify any other constant, it will default to a vibration.

I have put together a quick test repo together on GitHub, that includes a Swift-compatible API to use the taptic engine:

UIDevice.currentDevice().tapticEngine().actuateFeedback(UITapticEngineFeedbackPeek)

Use at your own risk!

I've also written a bit longer blog post, explaining this.



回答3:

In iOS 10 there's a new API called UIFeedbackGenerator. See this post for more details. It only works on the iPhone 7 for now.



回答4:

There doesn't seem to be a published API for iOS 9 currently.

On OSX you need to use NSHapticFeedbackManager:

NSHapticFeedbackManager Class Reference

and here is the API for WatchOS2:

WKInterfaceDevice Class Reference

By simply searching here you can see what I'm saying:

Haptic search (iOS pre-release) - shows nothing

Haptic search (OSX pre-release) - shows NSHapticFeedbackManager



标签: ios iphone ios9