3

In swift or objective-c, I can set exclusiveTouch property to true or YES, but how do I do it in swiftUI?

Alan Wu
  • 115
  • 9

2 Answers2

2

Xcode 11.3

Set exclusiveTouch and isMultipleTouchEnabled properties inside your struct init() or place it in AppDelegate.swift for the whole app:

struct ContentView: View {

init() {

        UIButton.appearance().isMultipleTouchEnabled = false
        UIButton.appearance().isExclusiveTouch = true

        UIView.appearance().isMultipleTouchEnabled = false
        UIView.appearance().isExclusiveTouch = true

        //OR

        for subView in UIView.appearance().subviews {

            subView.isMultipleTouchEnabled = false
            subView.isExclusiveTouch = true

            UIButton.appearance().isMultipleTouchEnabled = false
            UIButton.appearance().isExclusiveTouch = true
        }
    }

var body: some View {

VStack {
     Button(action: {
                print("BTN1")
            }){
                Text("First")
            }

     Button(action: {
                print("BTN2")
            }){
                Text("Second")
            }
   }  
 }
}
FRIDDAY
  • 3,781
  • 1
  • 29
  • 43
0

Can be handled something like below :

struct ContentView: View {

    @State private var isEnabled = false

    var body: some View {
        VStack(spacing: 50) {
            Button(action: {
                self.isEnabled.toggle()
            }) {
                Text("Button 1")
            }
            .padding(20)                
            .disabled(isEnabled)


            Button(action: {
                self.isEnabled.toggle()
            }) {
                Text("Button 2")
            }
            .padding(50)
            .disabled(!isEnabled)
        }
    }
}
Partha G
  • 1,056
  • 8
  • 13
  • Are you able to enable these buttons after clicking one of them? Adding a timer? – Alan Wu Nov 14 '19 at 02:46
  • Yes. Tapping the enabled button will make it disabled while enabling the other one. Regarding the timer, that is not part of the initial question. – Partha G Nov 14 '19 at 09:35
  • Both need to be enabled. And prevent multitouch. – FRIDDAY Dec 23 '19 at 09:57
  • @FRIDDAY Sample provided is to give an idea on how to enable one while the other should be in disabled state. Question has been changed since it was answered. – Partha G Dec 23 '19 at 16:26