SwiftUI的 TapGesture
记录一下SwiftUI的 TapGesture的使用方法:
import SwiftUI
struct GestureBootCamp: View {
@State var isClick = false
var body: some View {
VStack{
Button(action: {
isClick.toggle()
}, label: {
/*@START_MENU_TOKEN@*/Text("Button")/*@END_MENU_TOKEN@*/
.font(.headline)
.foregroundColor(.white)
.padding()
.padding(.horizontal, 50)
.background(.blue)
.cornerRadius(20)
})
Text("tap guesture")
.font(.headline)
.foregroundColor(.white)
.padding()
.padding(.horizontal, 50)
.background(.blue)
.cornerRadius(20)
// .onTapGesture {
// isClick.toggle()
// }
.onTapGesture(count: 2, perform: {
isClick.toggle()
})
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(isClick ? .red : .green)
}
}
#Preview {
GestureBootCamp()
}

本文介绍了如何在SwiftUI中使用TapGesture,包括创建一个名为GestureBootCamp的视图,其中包含一个按钮和一个响应双击事件的文本,通过@State变量isClick跟踪手势状态并改变背景颜色。
1665

被折叠的 条评论
为什么被折叠?



