游諭 Swift Dev 🦄

iOS 實機測試震動

每次時間不可改,最小間隔 0.5 秒

import AudioToolbox

AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))

套用 Swift 5.1 的 Property Wrapper

import AudioToolbox

@propertyWrapper
struct SoundId {
    typealias Value = UInt32
    var projectedValue: SystemSoundID
    var wrappedValue: Value {
        didSet {
            projectedValue = .init(self.wrappedValue)
        }
    }
    
    init(wrappedValue: Value) {
        self.projectedValue = SystemSoundID(wrappedValue)
        self.wrappedValue = wrappedValue
    }
}

class SOMECLASS {
    @SoundId var currentSystemEffect = kSystemSoundID_Vibrate
    
    func playSound() {
        AudioServicesPlaySystemSound(self.$currentSystemEffect)
    }
}
Tagged with: