Reputation: 434
Generally I want to get the haptic feedback that is produced when an icon is force pressed (peek/pop) within my SpriteKit game. Currently I am using 3d touch in the following way
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
if touch.force > CGFloat(6.66) {
shootOut ()
}
}
}
I would like the haptic feedback (similar to a peek or pop) to occur when if touch.force > CGFloat(6.66)
condition is satisfied. How can this be done?
Upvotes: 4
Views: 850
Reputation: 1113
if touch.force > CGFloat(6.66) {
You might want to use touch.maximumPossibleForce instead of hardcoded constant:
touch.force / touch.maximumPossibleForce
It's also might be a good idea to check whether force touch available or not:
if(is3dTouchAvailable(traitCollection: self.view!.traitCollection)) {
//...
}
More details and code sample http://www.mikitamanko.com/blog/2017/02/01/swift-how-to-use-3d-touch-introduction/
As for the exact same "effect" as for the icon force touch, here it is:
let generator = UIImpactFeedbackGenerator(style: .heavy)
generator.prepare()
generator.impactOccurred()
See more details about checking support and fallback for the haptic feedback: http://www.mikitamanko.com/blog/2017/01/29/haptic-feedback-with-uifeedbackgenerator/
Upvotes: 1
Reputation: 126177
The peek/pop haptic feedback can be done with UIPreviewInteraction
in iOS 10. (The docs for that are rather incomplete, but they also talk about it in a WWDC16 video.) This class is new in iOS 10, but works on both iPhone 6s and iPhone 7.
However, the idea with the peek/pop haptic feedback is that it should be tied to a UI that's doing something conceptually similar to peek/pop. (Even if that's a custom UI that can't get peek/pop haptics "for free".) Just like on Apple Watch, each haptic on iPhone has a meaning, and those meanings are more clear when you use them for the purposes they're designed for. (On the other hand, using UIPreviewInteraction
means that the system automatically handles for you the correlation between touch force and when to produce a haptic.)
So if you're looking for more general-purpose haptic feedback, in iOS 10 there's also a separate API for that, which provides several different kinds of haptics for use in a wider variety of situations. Check the docs for UIFeedbackGenerator
and play around with the options it provides and maybe you'll find one that feels right for your game.
UIFeedbackGenerator
requires both iOS 10 and iPhone 7 — it's specifically for the richer vocabulary of haptic signals available in the newer device's second-generation Taptic Engine, so it has no effect when used on iPhone 6s.
Upvotes: 2
Reputation: 10674
To get haptic feedback in your game you can use these
UIImpactFeedbackGenerator
UINotificationFeedbackGenerator
UISelectionFeedbackGenerator
e.g
// Impact
let generator = UIImpactFeedbackGenerator(style: .heavy)
generator.impactOccurred()
generator.prepare() // prepare for next time, should ideally call this before playing first time as well
// Selection
let generator = UISelectionFeedbackGenerator()
generator.selectionChanged()
generator.prepare() // prepare for next time, should ideally call this before playing first time as well
// Notification
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.warning)
generator.prepare() // prepare for next time, should ideally call this before playing first time as well
Just play around with all the generators and settings and see whats best for you.
Hope this helps
Upvotes: 0
Reputation: 536027
You are looking for UIPreviewInteraction and UIPreviewInteractionDelegate. Basically, it's a gesture recognizer for force touch. (Why they didn't actually make it a gesture recognizer beats me.) I've got an example of a force-touch based "game" where the user pops "bubbles" using force touch. This shows how you can measure and respond to the amount of force as a gesture.
Upvotes: 1