iOS 让手机振动的解决方案
May 1, 2023 — iOS
最近有一个需求,点击按钮时增加振动效果。
import UIKitimport AudioToolbox
class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // 这句话会使iPhone产生震动效果,可以加在按钮里面。 let soundID = SystemSoundID(kSystemSoundID_Vibrate) AudioServicesPlayAlertSoundWithCompletion(soundID) { } }}iOS 10 以上
let generator = UIImpactFeedbackGenerator(style: .heavy)generator.impactOccurred()其他的振动类型
import AudioToolboxAudioServicesPlaySystemSound(1519) // Actuate "Peek" feedback (weak boom)AudioServicesPlaySystemSound(1520) // Actuate "Pop" feedback (strong boom)AudioServicesPlaySystemSound(1521) // Actuate "Nope" feedback (series of three weak booms)enum Vibration { case error case success case warning case light case medium case heavy @available(iOS 13.0, *) case soft @available(iOS 13.0, *) case rigid case selection case oldSchool public func vibrate() { switch self { case .error: UINotificationFeedbackGenerator().notificationOccurred(.error) case .success: UINotificationFeedbackGenerator().notificationOccurred(.success) case .warning: UINotificationFeedbackGenerator().notificationOccurred(.warning) case .light: UIImpactFeedbackGenerator(style: .light).impactOccurred() case .medium: UIImpactFeedbackGenerator(style: .medium).impactOccurred() case .heavy: UIImpactFeedbackGenerator(style: .heavy).impactOccurred() case .soft: if #available(iOS 13.0, *) { UIImpactFeedbackGenerator(style: .soft).impactOccurred() } case .rigid: if #available(iOS 13.0, *) { UIImpactFeedbackGenerator(style: .rigid).impactOccurred() } case .selection: UISelectionFeedbackGenerator().selectionChanged() case .oldSchool: AudioServicesPlayAlertSoundWithCompletion(SystemSoundID(kSystemSoundID_Vibrate)){} } }}使用方法:
Vibration.success.vibrate()