我有一個視圖,DotView它由 3 個
綠色圓圈消失。

綠色圓圈停留。

綠色圓圈停留。
這是我的代碼:
/// 3 green circles, constrained to a red border
struct DotView: View {
var body: some View {
HStack {
ForEach(0..<3) { _ in
Circle()
.fill(Color.green)
.frame(width: 100, height: 100)
}
}
.frame(width: 250)
.border(Color.red)
.clipped() /// 1. make sure the green circles don't overflow
}
}
/// container for horizontal dragging, with a blue border
struct ContentView: View {
@State var offset = CGFloat(0)
var body: some View {
DotView()
.padding() /// add a small gap between the red and blue borders
.offset(x: offset, y: 0)
.border(Color.blue)
.clipped() /// 2. make sure `DotView` doesn't overflow the blue border
.gesture(
DragGesture(minimumDistance: 0) /// slide `DotView` left and right
.onChanged { offset = $0.translation.width }
)
}
}
消失的效果在使用 時非常明顯ForEach,但在其他視圖中也會發生。例如,這里是當你更換整個發生的事情HStack有Circle().fill(Color.green).frame(width: 100, height: 100):

有什么方法可以clipped多次使用而不會產生奇怪的副作用?為什么它們只在左側消失而不在右側消失?或者,offset是什么導致了問題?
uj5u.com熱心網友回復:
看起來像是繪圖優化(是的,看起來像是由offset)。
無論如何,這是一個修復 - 使用繪圖組修復內容(使用 Xcode 13 / iOS 15 測驗):
var body: some View {
DotView()
.padding()
.drawingGroup() // << here !!
.offset(x: offset, y: 0)
.border(Color.blue)
.clipped()
.gesture(
DragGesture(minimumDistance: 0) /// slide `DotView` left and right
.onChanged { offset = $0.translation.width }
)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/362382.html
標籤:ios 迅速 迅捷 swiftui-foreach
下一篇:如何防止ARSCNView旋轉
