3

My code below is setup so that I can press and hold my button to trigger an action and then stop that action when releasing the hold. How can I stop the user from triggering the gesture multiple times with different touches/fingers? In UIKit you can just disable multi-touch on an element but I could not find any equivalent syntax for SwiftUI.

struct playButton: View {
@EnvironmentObject var settings: TestParameters
@State var isDown: Bool = false
var db: Double
var dbString: String
var body: some View {
    let g = DragGesture(minimumDistance: 0, coordinateSpace: .local).onChanged({
        print("DOWN: \($0)")
        self.isDown = true

    }).onEnded({
        print("UP: \($0)")
        self.isDown = false

    })
    return ZStack{
        Rectangle()
            .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
            .gesture(g)
            .foregroundColor(isDown ? Color.init(hex: "6ca659"): Color.init(hex: "666699"))


        Text(dbString)
            .fontWeight(.bold)
            .foregroundColor(Color.white)
            .multilineTextAlignment(.center)
    }
  }
}
Luke Ireton
  • 479
  • 1
  • 3
  • 18
  • maybe this solves your problem: https://stackoverflow.com/questions/59363095/how-to-detect-and-take-action-when-a-button-is-being-pressed-in-swiftui – DoTryCatch Jan 11 '21 at 13:15

0 Answers0