iOS 振动的多种调用方式

在iOS上调用振动的多种方式,可以下载示例代码安装到真机感受。

enum Vibration: Int, CaseIterable {
    static var allCases: [Vibration] {
        return [.error, .success, .warning, .light, .medium, .heavy, .rigid, .soft, .selection, .oldSchool, .v1519, .v1520, .v1521]
    }

    case error = 0
    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
    case v1519 // weak
    case v1520 // strong
    case v1521 // 3 weak
    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:
            AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
        case .v1519:
            AudioServicesPlaySystemSound(1519)
        case .v1520:
            AudioServicesPlaySystemSound(1520)
        case .v1521:
            AudioServicesPlaySystemSound(1521)
        }
    }
}

github仓库地址 https://github.com/SunZhiC/iOS-Vibration

你可能感兴趣的:(iOS 振动的多种调用方式)