From d9cb692e4101f6133e99e6e54a3b86b50be4943e Mon Sep 17 00:00:00 2001 From: Daniel Storm Date: Fri, 20 Mar 2020 16:54:41 -0400 Subject: [PATCH] Add `prepare()` to reduce lag on initial haptic playback. --- vibrator/Vibrator/Vibrator.swift | 7 ++++++- vibrator/vibrator-demo/AppDelegate.swift | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/vibrator/Vibrator/Vibrator.swift b/vibrator/Vibrator/Vibrator.swift index adf8807..34e6490 100644 --- a/vibrator/Vibrator/Vibrator.swift +++ b/vibrator/Vibrator/Vibrator.swift @@ -52,7 +52,12 @@ public class Vibrator { private init() { guard supportsHaptics else { return } hapticEngine = try? CHHapticEngine() - try? hapticEngine?.start() + } + + /// Prepares the vibrator by acquiring hardware needed for vibrations. + public func prepare() { + guard let hapticEngine: CHHapticEngine = hapticEngine else { return } + try? hapticEngine.start() } // MARK: - Vibrate diff --git a/vibrator/vibrator-demo/AppDelegate.swift b/vibrator/vibrator-demo/AppDelegate.swift index c0cbfb9..5545245 100644 --- a/vibrator/vibrator-demo/AppDelegate.swift +++ b/vibrator/vibrator-demo/AppDelegate.swift @@ -12,6 +12,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // MARK: - UIApplicationDelegate func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { + Vibrator.shared.prepare() return true }