滑动识别 How to detect Swiping UP, DOWN, LEFT and RIGHT with SwiftUI on a View
1 2 3 4 5 6 7 8 9 10 .gesture(DragGesture (minimumDistance: 20 , coordinateSpace: .global).onEnded { value in let horizontalAmount = value.translation.width let verticalAmount = value.translation.height if abs (horizontalAmount) > abs (verticalAmount) { print (horizontalAmount < 0 ? "left swipe" : "right swipe" ) } else { print (verticalAmount < 0 ? "up swipe" : "down swipe" ) } })
拖拽手势 SwiftUI中的手势(DragGesture拖拽手势及Drag动画组件)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 struct DragGestureDemo : View { @GestureState private var dragOffset: CGSize = .zero @State private var position: CGSize = .zero var body: some View { ZStack { VStack { Text ("dragOffset: \(dragOffset) " ) Text ("position: \(position) " ) Spacer () } Text ("Drag me!" ) .font(.title) .padding() .background(Color .cyan) .cornerRadius(10.0 ) .offset(dragOffset) .offset(position) .gesture( DragGesture () .updating($dragOffset , body: { value, state, _ in state = value.translation }) .onEnded({ value in position.width += value.translation.width position.height += value.translation.height }) ) } } }