如何为 SwiftUI 制作滑动手势?

How to make Swipe gestures for SwiftUI?

在SwiftUI中有没有简单的手势识别方法?

据此tutorial我根据我的情况复制了代码:

(我需要从另一个视图更改值"page2")

import SwiftUI

struct SwipeGesture: UIViewRepresentable {

    @Binding var page2: Int

    func makeCoordinator() -> SwipeGesture.Coordinator {
        return SwipeGesture.Coordinator(parent1: self)
    }

    func makeUIView(context: UIViewRepresentableContext<SwipeGesture>) -> UIView {
        let view = UIView()
        view.backgroundColor = .clear
        let left = UISwipeGestureRecognizer(target: context.coordinator, action: #selector(context.coordinator.left))
        left.direction = .left

        let right = UISwipeGestureRecognizer(target: context.coordinator, action: #selector(context.coordinator.right))
        right.direction = .right

        view.addGestureRecognizer(left)
        view.addGestureRecognizer(right)
        return view
    }

    func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<SwipeGesture>) {

    }

    class Coordinator: NSObject{

        var parent : SwipeGesture

        init(parent1 : NSObject){
            parent = parent1
        }

        @objc func left(){
            print("left swipe")
            parent.page2 = parent.page2 + 1
        }

        @objc func right(){
            print("right swipe")
            parent.page2 = parent.page2 - 1
        }
    }
}

在 ContentView 中使用者:SwipeGesture(page2: $page2)

@State var page = 0 

用于主视图

@Binding var page: Int

是为了这个SwipeGesture struct 问题是:

知道我做错了什么吗?

SwiftUI 代码:

var body: some View {
// MARK: - Screen Layers
    ZStack {

        // Logo name
        VStack {
            Spacer()
                .frame(height: self.margin1)
            Image(uiImage: UIImage(named: "lobby_bg-logo")!)
                .renderingMode(.original)
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(height: self.screenH * 0.3125)
            Spacer()
        }
        SwipeGesture(page2: $page2)

错误:

Cannot assign value of type 'NSObject' to type 'SwipeGesture' Occurs in init part.

这里是修复

class Coordinator: NSObject{

    var parent : SwipeGesture

    init(parent1 : SwipeGesture){    // << corrected type !!
        parent = parent1
    }