crownSequencer 相当于 SwiftUI

问题描述 投票:0回答:1

我曾经使用 WKInterfaceController 的名为“crownSequencer”的属性来监听表冠的旋转(参见下面的代码)。我可以使用函数“crownDidRotate”获取表冠旋转的“rotationalValue”,如下所示。

import WatchKit
import Foundation

class InterfaceController: WKInterfaceController {

    override func awake(withContext context: Any?) {
        // Configure interface objects here.
    }

    override func willActivate() {
        // This method is called when watch view controller is about to be visible to user
    }

    override func didAppear() {
        super.didAppear()
        crownSequencer.delegate = self
        crownSequencer.focus()
    }

    override func didDeactivate() {
        // This method is called when watch view controller is no longer visible
    }

}

extension InterfaceController : WKCrownDelegate {
    func crownDidRotate(_ crownSequencer: WKCrownSequencer?, rotationalDelta: Double) {
        print(rotationalDelta)
    }
}

在 SwiftUI 中,我可以使用以下代码来监听视图中的表冠:



import SwiftUI
import SpriteKit

struct GameView: View {

    @State var scrollAmount: Float = 0.0

    // ...other code to initialise variables used

    var body: some View {
        SpriteView(scene: skScene!)
            .focusable(true)
            .digitalCrownRotation($scrollAmount, from: -70, through: 70, by: 0.1, sensitivity: .high, isContinuous: false, isHapticFeedbackEnabled: false)
            .onChange(of: scrollAmount) { newValue in
                //function )
            }
}

此代码侦听旋转并将“scrollAmount”绑定到状态变量。

但是,我似乎无法从原始的 CrownSequencer 方法中获取“rotationalDelta”的等价物。有没有办法在 SwiftUI 中获取“rotationalDelta”(即获取旋转的变化而不是滚动量,后者反映了当前的旋转级别)。

swiftui watchkit
1个回答
0
投票

如果我的理解正确,你只需要获得一个增量,如果我是对的,它可能对你有帮助:

.digitalCrownRotation(
                $scrollAmount,
                from: -100.0,
                through: 100.0,
                sensitivity: .low,
                isContinuous: true,
                isHapticFeedbackEnabled: true
            )
            .onChange(of: scrollAmount) { oldValue, newValue in
                let rotationalDelta = newValue - oldValue
                print(rotationalDelta)
            // any code
            }

您无法从 digitalCrownRotation() 获取增量,但可以使用 onChange() 来获取增量,更新后 onChange 可以使用 twozero 参数。 官方文档

© www.soinside.com 2019 - 2024. All rights reserved.