Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios 9 iphone 6S Play Haptic feedback or vibrate

Tags:

ios9

3dtouch

After user does force touch, I want to vibrate phone just like default behaviour.

Is it haptic? If so, how shall I do?

like image 211
Khant Thu Linn Avatar asked Sep 29 '15 10:09

Khant Thu Linn


3 Answers

Example in Swift (for iPhone 6S)

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)

Just in case - here're examples for iPhone 7/7+.

As for the force touch - you need to detect if it's available first:

func is3dTouchAvailable(traitCollection: UITraitCollection) -> Bool {
    return traitCollection.forceTouchCapability == UIForceTouchCapability.available
}

and then in touch events, it will be available as touch.force

func touchMoved(touch: UITouch, toPoint pos: CGPoint) {
    let location = touch.location(in: self)
    let node = self.atPoint(location)

    //...
    if is3dTouchEnabled {
        bubble.setPressure(pressurePercent: touch.force / touch.maximumPossibleForce)
    } else {
        // ...
    }
}

Here's my blog with a more detailed example with code samples:
http://www.mikitamanko.com/blog/2017/02/01/swift-how-to-use-3d-touch-introduction/

like image 58
Mikita Manko Avatar answered Sep 26 '22 21:09

Mikita Manko


Starting with iOS 10, there is a new public API for handling haptic feedback: UIFeedbackGenerator.

let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.success)

It is suggested to call .prepare() before using the generator and sending the feedback as there is a slight delay between the two due to the feedback hardware requiring a "wakeup". This could be done in viewDidLoad() or something comparable if you are expecting the feedback to be given shortly after.

See this blog for a good explanation of the new API and the available feedbacks:
https://www.hackingwithswift.com/example-code/uikit/how-to-generate-haptic-feedback-with-uifeedbackgenerator

For iOS 9 and older, you could use AudioToolBox as outlined in the other posts.

import AudioToolbox

private let isDevice = TARGET_OS_SIMULATOR == 0

func vibrate() {
    if isDevice {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
    }
}
like image 32
Frederik Winkelsdorf Avatar answered Sep 23 '22 21:09

Frederik Winkelsdorf


There are different feedback types. Try each one to figure out what is better for your needs:

// 1, 2, 3
let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.error)
generator.notificationOccurred(.success)
generator.notificationOccurred(.warning)

// 4
let generator = UIImpactFeedbackGenerator(style: .light)
generator.impactOccurred()

// 5
let generator = UIImpactFeedbackGenerator(style: .medium)
generator.impactOccurred()

// 6
let generator = UIImpactFeedbackGenerator(style: .heavy)
generator.impactOccurred()

// 7
let generator = UISelectionFeedbackGenerator()
generator.selectionChanged()
like image 30
Timur Bernikovich Avatar answered Sep 25 '22 21:09

Timur Bernikovich