如何使用 UISwipeGestureRecognizer 使用传递的数据重新加载相同的 viewcontroller

How to reload the same viewcontroller with passed data using UISwipeGestureRecognizer

有时很难为问题想出一个准确的标题...但是这里是:

我有一个 viewcontroller,我想 re-load 使用 UISwipeGestureRecognizer 使用新数据,我基本上想说“如果 UISwipeGestureRecognizer 方向向上,x = x + 1(我也会a x = x - 1 for a down swipe)。有些图像依赖于传递的整数,它们也会自动加载。理想情况下,它看起来就像滑动动画到新页面一样,但实际上它是相同的 viewcontroller 有不同的数据。

很难连贯地提出这个问题,但我希望有人能理解我正在尝试做的事情的要点。如果没有,请道歉。谢谢!

在您的主视图中添加两个滑动手势识别器,一个用于检测向上滑动,另一个用于检测向下滑动,如下所示:

class ViewController: UIViewController {

    // Your gesture recognizers:
    var swipeUpGestureRecognizer = UISwipeGestureRecognizer()
    var swipeDownGestureRecognizer = UISwipeGestureRecognizer()

    // Your x variable:
    var x = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        // Setup your gesture recognizers:
        swipeUpGestureRecognizer.direction = .up
        swipeUpGestureRecognizer.addTarget(self, action: #selector(swipeUpAction))

        swipeDownGestureRecognizer.direction = .down
        swipeDownGestureRecognizer.addTarget(self, action: #selector(swipeDownAction))

        // Inserting the gesture recognizers into the main view:
        self.view?.addGestureRecognizer(swipeUpGestureRecognizer)
        self.view?.addGestureRecognizer(swipeDownGestureRecognizer)

    }

    // Methods:
    func swipeUpAction() {
        // Update your data here!
        x = x + 1
    }

    func swipeDownAction() {
        // Update your data here!
        x = x - 1
    }

}

这应该可以正常工作!祝你好运!