我想从内容视图跳到ViewDrag,并通过拖动在View123之间跳转.但现在,从ViewDrag的预览中,我可以成功地拖动和跳转.但是如果我首先从内容视图跳到ViewDrag,我就不能再在ViewDrag中使用手势了! 我的最小代码如下:
@State var Jump = false
var body: some View {
NavigationStack {
VStack {
Button(action: {
Jump = true
}, label: {
Text("JUMP")
})
}
.navigationDestination(isPresented: $Jump, destination: {
ViewDrag()
})
}
}
}
struct ViewDrag: View {
@State private var currentPage = 0
@GestureState private var translation: CGFloat = 0
var body: some View {
NavigationStack {
if currentPage == 0 {
View1(nextPage: $currentPage)
.transition(.customTransition)
} else if currentPage == 1 {
View2(nextPage: $currentPage)
.transition(.customTransition)
} else if currentPage == 2 {
View3(nextPage: $currentPage)
.transition(.customTransition)
}
}
.gesture(
DragGesture()
.updating($translation, body: { value, state, _ in
state = value.translation.width
})
.onEnded({ value in
if value.translation.width < 0 && currentPage < 4 {
withAnimation {
currentPage += 1
}
} else if value.translation.width > 0 && currentPage > 0 && currentPage != 4 {
withAnimation {
currentPage -= 1
}
}
})
)
}
}
extension AnyTransition {
static var customTransition: AnyTransition {
let transition = AnyTransition.move(edge: .leading)
.combined(with: .opacity)
return .asymmetric(insertion: transition, removal: transition)
}
}
struct View1: View {
@Binding var nextPage: Int
@GestureState private var translation: CGFloat = 0
var body: some View {
Text("I am View1")
}
}
struct View2: View {
@Binding var nextPage: Int
@GestureState private var translation: CGFloat = 0
var body: some View {
Text("I am View2")
}
}
struct View3: View {
@Binding var nextPage: Int
@GestureState private var translation: CGFloat = 0
var body: some View {
Text("I am View3")
}
}
任何帮助或建议都将不胜感激!