Ios 多点触控可冻结快速用户界面拖动手势
我正在尝试在SwiftUI中拖动视图 直到我将第二个手指放在屏幕上,拖动才能正常工作,之后拖动停止,并且没有调用任何代码块(Ios 多点触控可冻结快速用户界面拖动手势,ios,swiftui,swift5,draggesture,Ios,Swiftui,Swift5,Draggesture,我正在尝试在SwiftUI中拖动视图 直到我将第二个手指放在屏幕上,拖动才能正常工作,之后拖动停止,并且没有调用任何代码块(onChanged,onned) 但是,当我再次开始用一根手指拖动时,它又开始工作了 有没有办法解决这个问题,或者我遗漏了什么 struct Pager: View { func drag(geometry: GeometryProxy) -> some Gesture { DragGesture() .onChanged({ (value) in
onChanged
,onned
)
但是,当我再次开始用一根手指拖动时,它又开始工作了
有没有办法解决这个问题,或者我遗漏了什么
struct Pager: View {
func drag(geometry: GeometryProxy) -> some Gesture {
DragGesture()
.onChanged({ (value) in
//some code
})
.onEnded({ (value) in
//some code
})
}
var body: some View {
GeometryReader { (geometry) in
ZStack {
ForEach(self.items.indices.reversed(), id: \.self) { index in
Card(index: index, item: self.items[index])
.offset(x: 0, y: self.offset(index: index, geometry: geometry))
.animation(.linear)
}
}
.background(Color.black)
.gesture(self.drag(geometry: geometry))
}
}
}
您可以使用与相同的解决方案,因为我相信这是相同的根本问题。解决方案使用了一个GestureState(如链接帖子中所述),它是一个临时值,当手势完成/中断时,它会返回到初始状态(0,0) 在使用GestureState时,您的代码应该是这样的(尽管您可以对其进行调整,以使用与原始帖子中类似的单独拖动方法):
这种方法非常有用: 因此,基本上,您可以将拖动手势与挤压和旋转手势组合在一起(同时使用) 当触发挤压或旋转时,您可以使用它发出取消拖动手势的信号,并相应地更新您的状态
struct Pager: View {
@GestureState private var dragPosition = CGSize.zero
var body: some View {
ZStack {
ForEach(self.items.indices.reversed(), id: \.self) { index in
Card(index: index, item: self.items[index])
.offset(x: 0, y: self.dragPosition.height) // This will now map the y offset of the card to the dragPosition GestureState. If you want the x offset use self.dragPosition.width
.animation(.linear)
}
}
.background(Color.black)
.gesture(
DragGesture()
.updating($dragPosition) { (value, gestureState, transaction) in
gestureState = CGSize(width: value.location.x - value.startLocation.x, height: value.location.y - value.startLocation.y)
}
.onChanged { value in
print("I'm changing")
}
.onEnded { value in
print("I've ended")
}
)
}
}