我可以将.onTapGesture用作嵌入视图的一部分,还是作为已嵌入视图的属性添加?看来这可行:
Text("Button")
.font(.system(size: buttonFontSizeSmaller))
.frame(width: buttonSize(false), height: buttonSize(false))
.foregroundColor(buttonLook.blackTextValue)
.background(buttonLook.lightGreyBkg)
.cornerRadius(buttonSize(false))
.onTapGesture {
print("Tapped!")
}
但是这个(我想工作的)不是:
由以下代码初始化:
struct ImageButton: View {
let buttonImageName: String
let buttonImageSize: CGFloat
let buttonSizeWidth: CGFloat
let buttonSizeHeight: CGFloat
let buttonTextColor: Color
let buttonColor: Color
var body: some View {
Image(systemName: buttonImageName)
.font(.system(size: buttonImageSize))
.frame(width: buttonSizeWidth, height: buttonSizeHeight)
.foregroundColor(buttonTextColor)
.background(buttonColor)
.cornerRadius(buttonSizeHeight)
.onTapGesture {
print("Tapped!")
}
}
并使用以下代码实现:
TextButton(buttonText: "AC", buttonFontSize: 34, buttonSizeWidth: 10, buttonSizeHeight: 10, buttonTextColor: Color.black, buttonColor: Color.red, brightness: 1.0)
如果我从引用结构中取出.onTapGesture并将其放在实现的最后一个主视图中,这似乎也不起作用...
有什么想法吗?那里有可以帮助的SwiftUI忍者吗?