如何在Apple Watch上使用SwiftUI实现简单的长按手势?

时间:2020-07-22 22:36:17

标签: swift swiftui apple-watch watchos-6

我一直在玩,虽然此代码在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)")
            }
        }
    }
}

0 个答案:

没有答案