在 Swift 或 Objective-C 中,我可以将 exclusiveTouch 属性设置为 true 或 YES,但在 SwiftUI 中该如何操作?
在 Swift 或 Objective-C 中,我可以将 exclusiveTouch 属性设置为 true 或 YES,但在 SwiftUI 中该如何操作?
Xcode 11.3
在你的结构体init()
内设置exclusiveTouch
和isMultipleTouchEnabled
属性,或将其放置在AppDelegate.swift
中,以适用于整个应用程序:
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")
}
}
}
}
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)
}
}
}