我正在嘗試實作向左/向右滑動功能,我希望用戶在視圖之間向左/向右滑動(在本例中為圓形和矩形)。雖然我有左/右檢測和視圖之間的切換作業,但我不確定如何實作它,以便當用戶向左拖動時,用戶會慢慢看到下一個視圖,直到它卡入到位。
這是它目前的樣子和代碼。
struct Swipe: View {
@State private var xOffset: CGFloat = 0.0
@State private var showCircle: Bool = false
@State private var showRectangle: Bool = true
var body: some View {
VStack {
Text("Current xOffset: \(xOffset)")
ZStack {
if showCircle {
Circle()
.frame(width: 250, height: 250)
}
if showRectangle {
Rectangle()
.foregroundColor(Color.red)
.frame(width: 100, height: 100)
.offset(x: xOffset)
}
}
.gesture(
DragGesture()
.onChanged {
value in
withAnimation(Animation.spring()) {
xOffset = value.translation.width
}
}
.onEnded { value in
if value.predictedEndLocation.x < -100 || value.predictedEndLocation.x > 100 {
showCircle.toggle()
showRectangle.toggle()
}
xOffset = 0.0
}
)
}
}
}

但我想要的是DragGesture. 我已經盡量把它畫得最好,在它過渡到下一個視圖時,黑色是背景。
任何幫助表示贊賞,謝謝。
uj5u.com熱心網友回復:
如果你想要你的影片像分頁,使用TabView:
TabView(selection: $selectedPage) {
ForEach(pageList, id: \.self) { page in
if page == .circle {
CircleView()
} else {
RectangleView()
}
}
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
記住如果 pageList 的型別是Array<T>selectedPage 的型別必須是T
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/334836.html
