How can I implement a simple long press gesture using SwiftUI on an Apple Watch?

1k views Asked by At

I've been playing around a bit and while this code works in iOs it won't work on WatchOS

import SwiftUI

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

When I say it "doesn't work" I mean doing the long press on an Apple Watch doesn't run the code within the closure block.

I'm thinking maybe .onLongPressGesture shouldn't be used with an Apple Watch, and there is some other method, but I can't figure out what it is (my understanding is force touch is being done away with in WatchOS 7.0).


edit: This is the actual code that's not working (it has a lot of methods attached to it, so it's hard to troubleshoot)

    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

There are 0 answers