Taptic in iOS 9

前端 未结 4 1541
-上瘾入骨i
-上瘾入骨i 2021-01-30 23:16

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 an

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-30 23:50

    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.

提交回复
热议问题