在垂直滚动时未调用collectionView上的向左/向右滑动

时间:2016-07-13 08:41:11

标签: ios swift uicollectionview uicollectionviewcell

我有collectionView垂直滚动,覆盖设备上的整个屏幕(即全屏)。

我已为Swipe Left and Right注册collectionView手势。

//------------right  swipe gestures in collectionView--------------//
    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.rightSwiped))
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    self.collectionView.addGestureRecognizer(swipeRight)

    //-----------left swipe gestures in collectionView--------------//
    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.leftSwiped))
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
    self.collectionView.addGestureRecognizer(swipeLeft)

问题:collectionView垂直滚动时,向左和向右滑动手势不会触发回调。

是否有任何简单的解决方法。

这是我的整个ViewController班级

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {




@IBOutlet weak var collectionView: UICollectionView!

let reuseIdentifier = "cell" // also enter this string as the cell identifier in the storyboard
var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48"]


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    collectionView.dataSource = self
    collectionView.delegate = self


    //------------right  swipe gestures in collectionView--------------//
    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.rightSwiped))
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    self.collectionView.addGestureRecognizer(swipeRight)

    //-----------left swipe gestures in collectionView--------------//
    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.leftSwiped))
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
    self.collectionView.addGestureRecognizer(swipeLeft)


}


func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.items.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    // get a reference to our storyboard cell
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CustomCell

    // Use the outlet in our custom class to get a reference to the UILabel in the cell
    cell.lable.text = self.items[indexPath.item]
    cell.backgroundColor = UIColor.yellowColor()

    return cell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    print(indexPath.row)
}

func rightSwiped()
{

    print("right swiped ")
}

func leftSwiped()
{

    print("left swiped ")

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

这是我的collectionView看起来像

enter image description here

编辑1

已解决,对于解决方案,请点击here

4 个答案:

答案 0 :(得分:5)

delegate的{​​{1}}默认手势识别器是集合视图对象本身(显然)。

UICollcetionView的默认实现在UICollectionView类的一侧返回-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

因此,要解决您的问题,您需要将集合视图对象设置为“左”和“右”滑动手势识别器的委托,如下所示。

YES

这会导致您的swipeRight.delegate = collectionView; swipeLeft.delegate = collectionView; rightSwiped()在相应的滑动发生时被触发。

答案 1 :(得分:0)

请尝试以下可能对您有帮助的代码。

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    // get a reference to our storyboard cell
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CustomCell

    cell.lable.text = self.items[indexPath.item]
    cell.backgroundColor = UIColor.yellowColor()
    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.leftSwiped))
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
    cell.addGestureRecognizer(swipeLeft)

    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.rightSwiped))
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    cell.addGestureRecognizer(swipeRight)

    return cell
}

答案 2 :(得分:0)

这是非常简单的解决方案

1)您需要使用属性存储以前的内容偏移量

2)实施委托方法Intent.FLAG_ACTIVITY_NEW_TASK&比较当前内容偏移量与先前内容偏移量

ScrollViewDidScroll

// MARK:UICollectionViewDelegate

var contentOffset: CGFloat = 0

3)可以在不添加任何手势识别器的情况下完成。

答案 3 :(得分:0)

感谢@Hariprasad指点我shouldRecognizeSimultaneouslyWithGestureRecognizer

这是解决方案

我有UICollectionView的子类并实现了UIGestureRecognizerDelegate,如下所示

import UIKit

class TouchCollectionView: UICollectionView, UIGestureRecognizerDelegate {

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
    // Drawing code
}
*/

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    let gesture = UIGestureRecognizer()
    gesture.delegate = self // Set Gesture delegate so that shouldRecognizeSimultaneouslyWithGestureRecognizer can be set to true on initialzing the UICollectionView
}

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}   
}

整个ViewController将保持与问题

中提到的相同