在垂直滚动时未调用collectionView上的向左/向右滑动
我有一个带有垂直滚动的collectionView
,覆盖设备上的整个屏幕(即全屏)。
我为我的collectionView
注册了Swipe Left and Right
手势。
//------------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
看起来像
编辑1
解决了,解决方案请点击这里
UICollcetionView
的默认手势识别器的UICollcetionView
是集合视图对象本身(显然)。
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
的默认实现-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
在UICollectionView类的一侧返回YES
。
因此,要解决您的问题,您需要将集合视图对象设置为“左”和“右”滑动手势识别器的委托,如下所示。
swipeRight.delegate = collectionView; swipeLeft.delegate = collectionView;
这应该使你的rightSwiped()
和leftSwiped()
在相应的滑动发生时被触发。
尝试以下可能对您有帮助的代码。
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 }
这是非常简单的解决方案
1)您需要使用属性来存储以前的内容偏移量
2)实现委托方法ScrollViewDidScroll
并将当前内容Offset与之前的内容Offset进行比较
var contentOffset: CGFloat = 0
// MARK:UICollectionViewDelegate
func scrollViewDidScroll(scrollView: UIScrollView) { if contentOffset > scrollView.contentOffset.y { // scrolling up } else if contentOffset < scrollView.contentOffset.y { //scrolling Down } contentOffset = scrollView.contentOffset.y }
3)可以在不添加任何手势识别器的情况下完成。
感谢@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 } }
- 从身份检查器http://prntscr.com/bsqbq3更改collectionView的类属性
整个ViewController将保持与问题中提到的相同