我正在尝试使用可以点击和拖动的元素来实现 ScrollView。它应该按以下方式工作:
下面的代码涵盖了所有这些要求(抽头指示器除外)。但是,我不确定它为什么起作用,具体来说,为什么我需要使用 .highPriorityGesture 并且例如不能对 Tap Gesture 和 DragGesture 进行排序.sequenced(before: ...)
(这会阻止滚动)。
另外,我希望在触地事件时收到通知(不是触地,参见 2.)。我尝试使用 LongPressGesture() 而不是 TapGesture(),但这也会阻止 ScrollView 滚动,之后甚至不会触发 DragGesture。
有人知道这是如何实现的吗?或者这就是 SwiftUI 的极限?如果是这样,是否有可能移植 UIKit 的东西来实现这一点(我也已经尝试过,但没有成功,ScrollView 的内容也应该是动态的,因此移植整个 ScrollView 可能很困难)?
谢谢你的协助!
struct ContentView: View {
var body: some View {
ScrollView() {
ForEach(0..<5, id: \.self) { i in
ListElem()
.highPriorityGesture(TapGesture().onEnded({print("tapped!")}))
.frame(maxWidth: .infinity)
}
}
}
}
struct ListElem: View {
@GestureState var dragging = CGSize.zero
var body: some View {
Circle()
.frame(width: 100, height: 100)
.gesture(DragGesture(minimumDistance: 0, coordinateSpace: .global)
.updating($dragging, body: {t, state, _ in
state = t.translation
}))
.offset(dragging)
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了几个选项,我认为组合sequenced
和simultaneously
允许两个手势同时运行。为了实现 onTouchDown,我使用了DragGesture
最小距离为 0 的 。
struct ContentView: View {
var body: some View {
ScrollView() {
ForEach(0..<5, id: \.self) { i in
ListElem()
.frame(maxWidth: .infinity)
}
}
}
}
struct ListElem: View {
@State private var offset = CGSize.zero
@State private var isDragging = false
@GestureState var isTapping = false
var body: some View {
// Gets triggered immediately because a drag of 0 distance starts already when touching down.
let tapGesture = DragGesture(minimumDistance: 0)
.updating($isTapping) {_, isTapping, _ in
isTapping = true
}
// minimumDistance here is mainly relevant to change to red before the drag
let dragGesture = DragGesture(minimumDistance: 0)
.onChanged { offset = $0.translation }
.onEnded { _ in
withAnimation {
offset = .zero
isDragging = false
}
}
let pressGesture = LongPressGesture(minimumDuration: 1.0)
.onEnded { value in
withAnimation {
isDragging = true
}
}
// The dragGesture will wait until the pressGesture has triggered after minimumDuration 1.0 seconds.
let combined = pressGesture.sequenced(before: dragGesture)
// The new combined gesture is set to run together with the tapGesture.
let simultaneously = tapGesture.simultaneously(with: combined)
return Circle()
.overlay(isTapping ? Circle().stroke(Color.red, lineWidth: 5) : nil) //listening to the isTapping state
.frame(width: 100, height: 100)
.foregroundColor(isDragging ? Color.red : Color.black) // listening to the isDragging state.
.offset(offset)
.gesture(simultaneously)
}
}
Run Code Online (Sandbox Code Playgroud)
归功于
/sf/answers/4192859121/ http://developer.apple.com/documentation/swiftui/composing-swiftui-gestures https://www.hackingwithswift.com/books/ios-swiftui/how-在swiftui中使用手势
归档时间: |
|
查看次数: |
2219 次 |
最近记录: |