gpt4 book ai didi

Color.clear 背景上的 SwiftUI onTapGesture 与 Color.blue 的行为不同

转载 作者:行者123 更新时间:2023-12-03 15:56:09 25 4
gpt4 key购买 nike

我正在定制 PickerSegmentedPickerStyle() .我想有相同的行为,但是当我点击内容和其中一个可能选择的边框之间的区域时,onTapGesture不起作用。当我添加蓝色背景时,它确实有效,但背景清晰时则无效。

使用蓝色背景

Working with blue background

无法在清晰的背景下工作

Not working with clear background

不工作的代码:

import SwiftUI

struct PickerElementView<Content>: View where Content : View {
@Binding var selectedElement: Int
let content: () -> Content

@inlinable init(_ selectedElement: Binding<Int>, @ViewBuilder content: @escaping () -> Content) {
self._selectedElement = selectedElement
self.content = content
}

var body: some View {
GeometryReader { proxy in
self.content()
.fixedSize(horizontal: true, vertical: true)
.frame(minWidth: proxy.size.width, minHeight: proxy.size.height)
// ##################################################################
// CHANGE COLOR HERE TO BLUE TO MAKE IT WORK
// ##################################################################
.background(Color.clear)
// ##################################################################
.border(Color.yellow, width: 5)

}
}

}

struct PickerView: View {
@Environment (\.colorScheme) var colorScheme: ColorScheme

var elements: [(id: Int, view: AnyView)]

@Binding var selectedElement: Int
@State var internalSelectedElement: Int = 0

private var width: CGFloat = 220
private var height: CGFloat = 100
private var cornerRadius: CGFloat = 20
private var factor: CGFloat = 0.95
private var color = Color(UIColor.systemGray)
private var selectedColor = Color(UIColor.systemGray2)


init(_ selectedElement: Binding<Int>) {
self._selectedElement = selectedElement
self.elements = [
(id: 0, view: AnyView(PickerElementView(selectedElement) {
Text("9").font(.system(.title))
})),
(id: 1, view: AnyView(PickerElementView(selectedElement) {
Text("5").font(.system(.title))

})),
]
self.internalSelectedElement = selectedElement.wrappedValue
}

func calcXPosition() -> CGFloat {
var pos = CGFloat(-self.width * self.factor / 4)
pos += CGFloat(self.internalSelectedElement) * self.width * self.factor / 2
return pos
}

var body: some View {
ZStack {
Rectangle()
.foregroundColor(self.selectedColor)
.cornerRadius(self.cornerRadius * self.factor)
.frame(width: self.width * self.factor / CGFloat(self.elements.count), height: self.height - self.width * (1 - self.factor))
.offset(x: calcXPosition())
.animation(.easeInOut(duration: 0.2))

HStack {
ForEach(self.elements, id: \.id) { item in
item.view
.gesture(TapGesture().onEnded { _ in
print(item.id)
self.selectedElement = item.id
withAnimation {
self.internalSelectedElement = item.id
}
})
}
}
}
.frame(width: self.width, height: self.height)
.background(self.color)
.cornerRadius(self.cornerRadius)
.padding()
}
}

struct PickerView_Previews: PreviewProvider {
static var previews: some View {
PickerView(.constant(1))
}
}



更改我标记的颜色。

有谁知道为什么他们的行为不同以及我如何解决这个问题?

最佳答案

一行答案不是设置backgroundColor,请设置contentShape用于 HitTest 。

 var body: some View {
GeometryReader { proxy in
self.content()
.fixedSize(horizontal: true, vertical: true)
.frame(minWidth: proxy.size.width, minHeight: proxy.size.height)
// ##################################################################
// CHANGE COLOR HERE TO BLUE TO MAKE IT WORK
// ##################################################################
.contentShape(Rectangle())
// ##################################################################
.border(Color.yellow, width: 5)

}
}

关于Color.clear 背景上的 SwiftUI onTapGesture 与 Color.blue 的行为不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58696455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com