Swift 3 - 向右滑动时关闭视图控制器

时间:2016-11-05 14:17:27

标签: ios iphone swift uiviewcontroller

当用户在收集视图后面滑动以返回主屏幕时,我试图解除VC。它是一个简单的报价应用程序,我使用集合视图在页面上显示每个引用(总共30个)。目前,我可以检测滑动并从collectionView类中获取正确的indexPath.row计数。我只是无法弄清楚如何检测报价为0,如果用户再次向右滑动则忽略当前的VC。目前的代码如下:

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return rules.rulesArray.count

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let randomImage = Int(arc4random_uniform(UInt32(10)))

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! myCollectionViewCell

    // Cell text
    cell.ruleLabel.text = rules.rulesArray[indexPath.row].uppercased()

    // Change image on specific quotes
        cell.ruleImage.image = UIImage(named: "Background\(randomImage)")

    //detectCell(cell: indexPath.row)

    return cell

}

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {

    print(indexPath.row)

    // Swipe handling
    if indexPath.row == 0 {

        performSegue(withIdentifier: "homeView", sender: nil)
    }
}

我在viewDidLoad中有滑动声明,并且是:

var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.right
    self.view.addGestureRecognizer(swipeRight)

外面的功能是我曾多次使用的功能:

func respondToSwipeGesture(gesture: UIGestureRecognizer) {

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {

        switch swipeGesture.direction {
        case UISwipeGestureRecognizerDirection.right:
            print("Swiped right")
        case UISwipeGestureRecognizerDirection.down:
            print("Swiped down")
        case UISwipeGestureRecognizerDirection.left:
            print("Swiped left")
        case UISwipeGestureRecognizerDirection.up:
            print("Swiped up")
        default:
            break
        }
    }
}

我确实试图创建一个检测单元格函数,但发现collectionView有一个我认为可以使用的willDisplay挂钩。

感谢。

0 个答案:

没有答案