1

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

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

问题在于使用触觉的设备。现在我使用风格 aswarning 但设备振动非常微弱,甚至不明显。是否有另一种方法可以使设备振动 2 次或更强烈?

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

4

1 回答 1

0

CoreHaptic 中有执行此操作的功能,这里是教程

// 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
    }
}
于 2021-02-08T20:31:46.667 回答