0

As title says I have created a timer to publish every second and on the 5th second it disables a picker in my view. The timer itself is started by tapping on the picker and it works on the first try but doesn't connect on subsequent tries.

I am trying to create one of those web elements that can be dragged across the screen and placed wherever to clear users view but to allow for this I have to disable the picker or else it prevents other gestures from working. What is the problem?

struct ContentView: View { // some other parameters @State private var isSorting = false let timer = Timer.publish(every: 1, on: .main, in: .common) @State private var count = 0 func timeOut() { if count >= 5 { isSorting = false timer.connect().cancel() count = 0 } count += 1 } var body: some View { // other gesture code ZStack(alignment: .init(horizontal: .trailing, vertical: .bottom)) { TabView { // tabs } VStack { // spacers // if code VStack { Menu { // picker } label: { Image(systemName: "arrow.up.arrow.down") } } .disabled(!isSorting) .onTapGesture { isSorting = true timer.connect() } .onReceive(timer) { _ in timeOut() } // .other modifiers } } } } 
1
  • Hi, instead of publish, use Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(foobar), userInfo: nil, repeats: true) Commented Jun 28 at 11:06

1 Answer 1

0

So I figured I could just auto connect and let the function run irrespective of being the tap gesture being triggered. Hopefully this isn't resource wasting. Code below:

struct ContentView: View { // some other parameters @State private var isSorting = false let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect() @State private var count = 0 func timeOut() { if count >= 5 { isSorting = false count = 0 } count += 1 } var body: some View { // other gesture code ZStack(alignment: .init(horizontal: .trailing, vertical: .bottom)) { TabView { // tabs } VStack { // spacers // if code VStack { Menu { // picker } label: { Image(systemName: "arrow.up.arrow.down") } } .disabled(!isSorting) .onTapGesture { isSorting = true } .onReceive(timer) { _ in timeOut() } // .other modifiers } } } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.