0

我一直在玩,虽然这段代码在 iOs 中有效,但在 WatchOS 上无效

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Hello, World!")
            .onTapGesture {
                print("tapped!")
        }
        .onLongPressGesture {
            print("Long pressed!")
        }
    }
}

当我说它“不起作用”时,我的意思是在 Apple Watch 上长按不会运行闭包块内的代码。

我在想也许.onLongPressGesture不应该与 Apple Watch 一起使用,还有其他一些方法,但我不知道它是什么(我的理解是在 WatchOS 7.0 中取消了强制触摸)。


编辑:这是不起作用的实际代码(它附加了很多方法,因此很难排除故障)

    var body: some View {
        Start()
            .background(NavigationLink(destination: RunSelector(), isActive: self.$showRunSelectorList) {EmptyView()})
            .background(NavigationLink(destination: RunningTimer(myIntervals: allIntervals.week1d1), isActive: self.$showRunningTimer) {EmptyView()})
            .buttonStyle(PlainButtonStyle())
            .onTapGesture {
                self.showRunningTimer = true
        }
        .onLongPressGesture {
            print("Long pressed!")
            self.showRunSelectorList = true
        }
        .focusable(true)
        .digitalCrownRotation(self.$crownValue, from: 0.1, through: 5.0, sensitivity: .low, isContinuous: false, isHapticFeedbackEnabled: true)
        .onAppear() {
            HealthKitSetupAssistant.authorizeHealthKit { (authorized, error) in
                guard authorized else {
                    let baseMessage = "HealthKit Authorization Failed"
                    if let error = error {
                        print("\(baseMessage). Reason: \(error.localizedDescription)")
                    } else {
                        print(baseMessage)
                    }
                    return
                }
                
                print("HealthKit Successfully Authorized.")
            }
        }.onReceive(Just(self.crownValue)) { output in
            if self.crownValue > 0.1 {
                self.showRunSelectorList = true
                self.crownValue = 0.0
            } else {
                print("The value is \(self.crownValue)")
            }
        }
    }
}
4

0 回答 0