Created
December 5, 2020 17:01
-
-
Save shafayeatsumit/96c7f150cf0533baecd5e070d58d319b to your computer and use it in GitHub Desktop.
IOS CoreHaptics module for ReactNative.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SoftVibration.swift | |
// MyTestApp | |
// | |
// Created by Shafayeat Kabir on 5/12/20. | |
// | |
import Foundation | |
import CoreHaptics | |
@available(iOS 13.0, *) | |
@objc(SoftVibration) | |
class SoftVibration: NSObject { | |
private var engine: CHHapticEngine! | |
private var continuousPlayer: CHHapticPatternPlayer! | |
@objc | |
func stopVibrationIOS() { | |
do { | |
try continuousPlayer.stop(atTime: CHHapticTimeImmediate) | |
} catch let error { | |
print("Error stopping the continuous haptic player: \(error)") | |
} | |
} | |
@objc | |
func prepareHaptics(){ | |
guard CHHapticEngine.capabilitiesForHardware().supportsHaptics else {return} | |
do{ | |
self.engine = try CHHapticEngine(); | |
try engine?.start() | |
print("SUCCESS") | |
}catch{ | |
print("there was an error engine") | |
} | |
} | |
@objc(startVibrationIOS:) | |
func startVibrationIOS(duration: Int) -> Void { | |
print("Duration \(duration)") | |
var events = [CHHapticEvent]() | |
let intensity = CHHapticEventParameter(parameterID: .hapticIntensity, value: 0.3) | |
let sharpness = CHHapticEventParameter(parameterID: .hapticSharpness, value: 1) | |
let event = CHHapticEvent(eventType: .hapticContinuous, parameters: [intensity, sharpness], relativeTime: 0, duration: TimeInterval(duration)) | |
events.append(event) | |
do{ | |
let pattern = try CHHapticPattern(events: events, parameters: []) | |
continuousPlayer = try engine?.makePlayer(with: pattern) | |
try continuousPlayer?.start(atTime: 0) | |
}catch{ | |
print("Failed to play: \(error.localizedDescription)") | |
} | |
} | |
@objc | |
static func requiresMainQueueSetup() -> Bool { | |
return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
//SoftVibration.m