Ana içeriğe geç

İşlev Atama

Bir DeviceFunction değerini desteklenen bir DeviceOperation değerine atayın.

Ön Koşullar

  • Cihaz bağlı ve hazırdır
  • Cihaz DevicePhysicalOperationsAPI protokolünü destekler
  • İşlem physicalOperationsMapping içinde bulunur

API Referansı

Framework

AIBuds.xcframework

İçe Aktarma

Swift
import AIBuds

Protokol

Swift
/// The protocol for device physical operations API.
protocol DevicePhysicalOperationsAPI: DeviceAPI {
    /// Assign a device function to a specific operation.
    ///
    /// - Parameters:
    ///   - function: The ``DeviceFunction`` to be assigned.
    ///   - operation: The ``DeviceOperation`` that will trigger the function.
    ///   - completion: An optional callback invoked when the assignment completes.
    ///     - success: `true` if the assignment succeeded; `false` otherwise.
    ///     - error: An error object explaining the failure, or `nil` on success.
    func assignFunction(
        _ function: DeviceFunction,
        forPhysicalOperation operation: DeviceOperation,
        completion: AIBudsCompletionHandler?
    )
}

Örnek Yöntemi

Fiziksel bir işleme işlev atar.

Swift
/// Assign a device function to a specific operation.
///
/// - Parameters:
///   - function: The ``DeviceFunction`` to be assigned.
///   - operation: The ``DeviceOperation`` that will trigger the function.
///   - completion: An optional callback invoked when the assignment completes.
///     - success: `true` if the assignment succeeded; `false` otherwise.
///     - error: An error object explaining the failure, or `nil` on success.
func assignFunction(
    _ function: DeviceFunction,
    forPhysicalOperation operation: DeviceOperation,
    completion: AIBudsCompletionHandler?
)

Parametreler

ParametreTürAçıklama
functionDeviceFunctionAtanacak işlev.
operationDeviceOperationİşlevi tetikleyen fiziksel işlem.
completionAIBudsCompletionHandler?İsteğe bağlı completion handler.

Callback Parametreleri:

AdTürAçıklama
successBool / BOOLAtamanın başarılı olup olmadığı.
errorNSError?Hata ayrıntıları; başarılıysa nil.

Dönüş Değeri

Bu yöntem doğrudan değer döndürmez.

Kullanım Örnekleri

Swift
import AIBuds

guard let device = device as? DevicePhysicalOperationsAPI else {
    print("Device does not support physical-operation mapping")
    return
}

device.assignFunction(
    .playPause,
    forPhysicalOperation: .rightDoubleClick
) { success, error in
    guard success else {
        print("Assignment failed: \(error?.localizedDescription ?? "Unknown error")")
        return
    }
    print("Function assigned")
}

Hata Yönetimi

success değerini denetleyin ve hata ayrıntıları için error kullanın. Completion başarı bildirmeden yerel eşlemeyi güncellemeyin.

En İyi Uygulamalar

  1. Bildirilen Eşlemeden Başlayın: physicalOperationsMapping içinde bildirilen işlemleri sunun.
  2. SDK Enum'larını Kullanın: Rastgele tamsayılar yerine DeviceOperation ve DeviceFunction iletin.
  3. Başarıdan Sonra Yenileyin: Uygulanan yapılandırmayı gösterirken eşlemeyi yeniden okuyun.

Notlar

  • API her çağrıda bir işleme tek bir işlev atar.
  • SDK bir KeyMapping modeli veya customizeKeyMapping yöntemi tanımlamaz.