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

时间:2017-05-15 21:12:25

标签: swift swipe viewcontroller uiswipegesturerecognizer

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

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

很难以连贯的方式提出这个问题,但我希望有人可以得到我想要做的事情的要点。如果没有,请道歉。谢谢!

1 个答案:

答案 0 :(得分:0)

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

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
    }

}

这应该工作得很好!祝好运!