如何在 SwiftUI 中实现触发切换案例的左或右 DragGesture()?
How to implement a left or right DragGesture() that trigger a switch case in SwiftUI?
我在视图中创建了一个 DragGesture,无论用户向左还是向右滑动,它都应该 select @State(Bool)。
问题是只检测到向右滑动。
如何使用 .gesture() 捕捉用户是在屏幕上向左滑动还是向右滑动?
import SwiftUI
struct SwiftUIView: View {
//MARK: Value to change on swipe gesture
@State var swipeRight: Bool
var body: some View {
VStack {
//MARK: Value displayed after user swiped
Text($swipeRight ? "Right" : "Left")
}
.gesture(
DragGesture()
.onChanged { (value) in
//MARK: What is it missing here?
switch value.location.x {
case ...(-0.5):
self.swipeRight = false
print("Swipe Left return false")
case 0.5...:
self.swipeRight = true
print("Swipe Right return true")
default: ()
}
})
}
您应该比较新旧位置:
if value.startLocation.x > value.location.x {
print("Swipe Left")
} else {
print("Swipe Right")
}
因此您的代码的重构版本将是:
struct ContentView: View {
enum SwipeHorizontalDirection: String {
case left, right, none
}
@State var swipeHorizontalDirection: SwipeHorizontalDirection = .none { didSet { print(swipeHorizontalDirection) } }
var body: some View {
VStack {
Text(swipeHorizontalDirection.rawValue)
}
.gesture(
DragGesture()
.onChanged {
if [=11=].startLocation.x > [=11=].location.x {
self.swipeHorizontalDirection = .left
} else if [=11=].startLocation.x == [=11=].location.x {
self.swipeHorizontalDirection = .none
} else {
self.swipeHorizontalDirection = .right
}
})
}
}
Swift5,iOS13
[Mojtaba Hosseini][1] 在这里展示的改进版本。
[1]: https://whosebug.com/users/5623035/mojtaba-hosseini。将枚举和函数放在 ContentView 的主体之前。
enum SwipeHVDirection: String {
case left, right, up, down, none
}
func detectDirection(value: DragGesture.Value) -> SwipeHVDirection {
if value.startLocation.x < value.location.x - 24 {
return .left
}
if value.startLocation.x > value.location.x + 24 {
return .right
}
if value.startLocation.y < value.location.y - 24 {
return .down
}
if value.startLocation.y > value.location.y + 24 {
return .up
}
return .none
}
...
在 DragGesture 中调用它。在 onEnded 上调用它以停止它多次触发。
.gesture(DragGesture()
.onEnded { value in
print("value ",value.translation.width)
let direction = self.detectDirection(value: value)
if direction == .left {
// your code here
}
}
)
显然您 need/can 根据需要添加其他方向...
我觉得上面的方向颠倒了?或者我误解了左右滑动。无论如何,这是使用视图修改器的进一步改进。您可以直接在 .onSwipe { direction in ... } 中查看您的观点。
struct SwipeModifier: ViewModifier {
let action: ((UISwipeGestureRecognizer.Direction) -> Void)?
init(perform action: ((UISwipeGestureRecognizer.Direction) -> Void)? = nil) {
self.action = action
}
func body(content: Content) -> some View {
content
.gesture(DragGesture(minimumDistance: 24.0, coordinateSpace: .local)
.onEnded { value in
guard let action = action else {
return
}
if value.startLocation.x > value.location.x {
action(.left)
} else if value.startLocation.x < value.location.x {
action(.right)
} else if value.startLocation.y > value.location.y {
action(.down)
} else if value.startLocation.y < value.location.y {
action(.up)
}
})
}
}
extension View {
public func onSwipe(perform action: ((UISwipeGestureRecognizer.Direction) -> Void)? = nil) -> some View {
return self.modifier(SwipeModifier(perform: action))
}
}
扩展 DragGesture.Value
:
import SwiftUI
extension DragGesture.Value {
func detectDirection(_ tolerance: Double = 24) -> Direction? {
if startLocation.x < location.x - tolerance { return .left }
if startLocation.x > location.x + tolerance { return .right }
if startLocation.y > location.y + tolerance { return .up }
if startLocation.y < location.y - tolerance { return .down }
return nil
}
enum Direction {
case left
case right
case up
case down
}
}
向 View
添加一个 dismissingGesture 方法:
extension View {
public func dismissingGesture(tolerance: Double = 24, action: @escaping () -> ()) -> some View {
gesture(DragGesture()
.onEnded { value in
let direction = value.detectDirection(tolerance)
if direction == .left {
action()
}
}
)
}
}
用法示例:
struct DismissingGesture_Previews: PreviewProvider {
static var previews: some View {
Preview()
}
struct Preview: View {
var body: some View {
NavigationView {
NavigationLink {
Destination()
} label: {
Text("Go")
}
}
}
}
struct Destination: View {
@Environment(\.dismiss) private var dismiss
var body: some View {
ZStack {
Color.yellow.edgesIgnoringSafeArea(.all)
Text("Nothing")
}
.dismissingGesture {
dismiss()
}
.navigationBarHidden(true)
}
}
}
我在视图中创建了一个 DragGesture,无论用户向左还是向右滑动,它都应该 select @State(Bool)。
问题是只检测到向右滑动。
如何使用 .gesture() 捕捉用户是在屏幕上向左滑动还是向右滑动?
import SwiftUI
struct SwiftUIView: View {
//MARK: Value to change on swipe gesture
@State var swipeRight: Bool
var body: some View {
VStack {
//MARK: Value displayed after user swiped
Text($swipeRight ? "Right" : "Left")
}
.gesture(
DragGesture()
.onChanged { (value) in
//MARK: What is it missing here?
switch value.location.x {
case ...(-0.5):
self.swipeRight = false
print("Swipe Left return false")
case 0.5...:
self.swipeRight = true
print("Swipe Right return true")
default: ()
}
})
}
您应该比较新旧位置:
if value.startLocation.x > value.location.x {
print("Swipe Left")
} else {
print("Swipe Right")
}
因此您的代码的重构版本将是:
struct ContentView: View {
enum SwipeHorizontalDirection: String {
case left, right, none
}
@State var swipeHorizontalDirection: SwipeHorizontalDirection = .none { didSet { print(swipeHorizontalDirection) } }
var body: some View {
VStack {
Text(swipeHorizontalDirection.rawValue)
}
.gesture(
DragGesture()
.onChanged {
if [=11=].startLocation.x > [=11=].location.x {
self.swipeHorizontalDirection = .left
} else if [=11=].startLocation.x == [=11=].location.x {
self.swipeHorizontalDirection = .none
} else {
self.swipeHorizontalDirection = .right
}
})
}
}
Swift5,iOS13
[Mojtaba Hosseini][1] 在这里展示的改进版本。
[1]: https://whosebug.com/users/5623035/mojtaba-hosseini。将枚举和函数放在 ContentView 的主体之前。
enum SwipeHVDirection: String {
case left, right, up, down, none
}
func detectDirection(value: DragGesture.Value) -> SwipeHVDirection {
if value.startLocation.x < value.location.x - 24 {
return .left
}
if value.startLocation.x > value.location.x + 24 {
return .right
}
if value.startLocation.y < value.location.y - 24 {
return .down
}
if value.startLocation.y > value.location.y + 24 {
return .up
}
return .none
}
...
在 DragGesture 中调用它。在 onEnded 上调用它以停止它多次触发。
.gesture(DragGesture()
.onEnded { value in
print("value ",value.translation.width)
let direction = self.detectDirection(value: value)
if direction == .left {
// your code here
}
}
)
显然您 need/can 根据需要添加其他方向...
我觉得上面的方向颠倒了?或者我误解了左右滑动。无论如何,这是使用视图修改器的进一步改进。您可以直接在 .onSwipe { direction in ... } 中查看您的观点。
struct SwipeModifier: ViewModifier {
let action: ((UISwipeGestureRecognizer.Direction) -> Void)?
init(perform action: ((UISwipeGestureRecognizer.Direction) -> Void)? = nil) {
self.action = action
}
func body(content: Content) -> some View {
content
.gesture(DragGesture(minimumDistance: 24.0, coordinateSpace: .local)
.onEnded { value in
guard let action = action else {
return
}
if value.startLocation.x > value.location.x {
action(.left)
} else if value.startLocation.x < value.location.x {
action(.right)
} else if value.startLocation.y > value.location.y {
action(.down)
} else if value.startLocation.y < value.location.y {
action(.up)
}
})
}
}
extension View {
public func onSwipe(perform action: ((UISwipeGestureRecognizer.Direction) -> Void)? = nil) -> some View {
return self.modifier(SwipeModifier(perform: action))
}
}
扩展 DragGesture.Value
:
import SwiftUI
extension DragGesture.Value {
func detectDirection(_ tolerance: Double = 24) -> Direction? {
if startLocation.x < location.x - tolerance { return .left }
if startLocation.x > location.x + tolerance { return .right }
if startLocation.y > location.y + tolerance { return .up }
if startLocation.y < location.y - tolerance { return .down }
return nil
}
enum Direction {
case left
case right
case up
case down
}
}
向 View
添加一个 dismissingGesture 方法:
extension View {
public func dismissingGesture(tolerance: Double = 24, action: @escaping () -> ()) -> some View {
gesture(DragGesture()
.onEnded { value in
let direction = value.detectDirection(tolerance)
if direction == .left {
action()
}
}
)
}
}
用法示例:
struct DismissingGesture_Previews: PreviewProvider {
static var previews: some View {
Preview()
}
struct Preview: View {
var body: some View {
NavigationView {
NavigationLink {
Destination()
} label: {
Text("Go")
}
}
}
}
struct Destination: View {
@Environment(\.dismiss) private var dismiss
var body: some View {
ZStack {
Color.yellow.edgesIgnoringSafeArea(.all)
Text("Nothing")
}
.dismissingGesture {
dismiss()
}
.navigationBarHidden(true)
}
}
}