如何让设备振动更剧烈?

问题描述

现在我有一个扩展程序可以让设备在应用程序的任何地方振动:

extension UIDevice {
    static func vibrate(style: UINotificationFeedbackGenerator.FeedbackType) {
        guard CHHapticEngine.capabilitiesForHardware().supportsHaptics else {
            AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
            return
        }
        let generator = UINotificationFeedbackGenerator()
        generator.notificationOccurred(style)
    }
}

问题在于使用触觉的设备。现在我使用样式作为 warning 但设备振动非常微弱,甚至不明显。有没有其他方法可以让设备振动 2 次或更强烈?

我在代码中尝试了不同的振动方式,但振动仍然很弱。

解决方法

CoreHaptic 中有一个函数可以做到这一点,这里是 tutorial

// Create an intensity parameter:
let intensity = CHHapticEventParameter(parameterID: .hapticIntensity,value: initialIntensity)

// Create a sharpness parameter:
let sharpness = CHHapticEventParameter(parameterID: .hapticSharpness,value: initialSharpness)

// Create a continuous event with a long duration from the parameters.
let continuousEvent = CHHapticEvent(eventType: .hapticContinuous,parameters: [intensity,sharpness],relativeTime: 0,duration: 100)

do {
    // Create a pattern from the continuous haptic event.
    let pattern = try CHHapticPattern(events: [continuousEvent],parameters: [])
    
    // Create a player from the continuous haptic pattern.
    continuousPlayer = try engine.makeAdvancedPlayer(with: pattern)
    
} catch let error {
    print("Pattern Player Creation Error: \(error)")
}

continuousPlayer.completionHandler = { _ in
    DispatchQueue.main.async {
        // Restore original color.
        self.continuousPalette.backgroundColor = self.padColor
    }
}