简体   繁体   English

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

[英]Swipe Left/Right on collectionView not called while scrolling vertically

I have a collectionView with vertical scrolling, covering whole screen on the device (ie fullscreen).我有一个垂直滚动的collectionView ,覆盖设备上的整个屏幕(即全屏)。

I have register the Swipe Left and Right gestures for my 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)

Problem: Swipe left and right gestures callback does not fire while collectionView is scrolling vertically.问题:collectionView垂直滚动时,左右滑动手势回调不会触发。

Is there any simple workaround for this.是否有任何简单的解决方法。

here is my whole ViewController Class这是我的整个ViewController Class

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.
}


}

here is my collectionView look like这是我的collectionView的样子

在此处输入图像描述

EDIT 1编辑 1

Solved, for solutions click here已解决,请点击此处查看解决方案

The delegate of default gesture recognisers of the UICollcetionView is collection view object itself (obviously). delegate的的默认手势识别器UICollcetionView是集合视图对象本身(明显)。

The default implementation of -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer returns YES in side the UICollectionView class. -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer的默认实现-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer在UICollectionView类的一侧返回YES

So to address your problem ,you need to set the collection view object as delegate to your "left" and "right" swipe gesture recognisers as follows. 因此,要解决您的问题,您需要将集合视图对象设置为“左”和“右”滑动手势识别器的委托,如下所示。

swipeRight.delegate = collectionView;
swipeLeft.delegate = collectionView;

This should make your rightSwiped() and leftSwiped() to get fired when corresponding swipe occurs. 这会使你的rightSwiped()leftSwiped()在相应的滑动发生时被触发。

Thanks @Hariprasad for pointing me shouldRecognizeSimultaneouslyWithGestureRecognizer 感谢@Hariprasad指出我shouldRecognizeSimultaneouslyWithGestureRecognizer

Here is the solution 这是解决方案

I have subclass the UICollectionView and implemented the UIGestureRecognizerDelegate like below 我有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
}   
}

Whole ViewController will remain the same as mentioned in the question 整个ViewController将保持与问题中提到的相同

Try following code it may helps you. 尝试以下可能对您有帮助的代码。

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
}

Here is very simple Solution 这是非常简单的解决方案

1)You need take a property to store previous content offset 1)您需要使用属性来存储以前的内容偏移量

2)Implement the delegate method ScrollViewDidScroll & compare current content Offset with previous content Offset 2)实现委托方法ScrollViewDidScroll并将当前内容Offset与之前的内容Offset进行比较

var contentOffset: CGFloat = 0

// MARK: UICollectionViewDelegate // 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)It can be done without adding any gesture recognizer. 3)可以在不添加任何手势识别器的情况下完成。

Swift 5 This help me. Swift 5 这对我有帮助。

class OnboardingVC: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UICollectionViewDelegate, UIGestureRecognizerDelegate {
    
@IBOutlet private var collectionView: UICollectionView!
            
     override func viewDidLoad() {
        let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(leftSwipeAction))
        leftSwipe.direction = .left
        leftSwipe.delegate = self
        collectionView.addGestureRecognizer(leftSwipe)
     }
            
     func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
       return true
     }
            
     @objc private func leftSwipeAction() {
     print("Left Swipe")
     }
} 
    
       

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 左右滑动手势可水平滚动tableview - Horizontal scrolling of tableview with swipe gesture left and right UISplitViewControllerDelegate / UIPopoverDelegate方法在从右向左滑动时未调用 - UISplitViewControllerDelegate/UIPopoverDelegate methods not called on right-to-left swipe 滚动时CollectionView滞后 - CollectionView lag while scrolling 快速向左或向右滑动 - Swipe Left or Right in swift UITableView从左向右滑动 - UITableView swipe left to right 单击左侧和右侧按钮的同时将CollectionView的单元格滚动到左右 - Scroll Cell of CollectionView to Left and Right While Clicking On left&right Button UITableviewCell 滑动不起作用,父 collectionView 滚动代替 - UITableviewCell swipe not work, parent collectionView scrolling instead iOS如何检测UICollectionView上向左或向右滑动,否则只能垂直滚动? - iOS how to detect swipe left or right on UICollectionView that otherwise only scrolls vertically? 结束后向左滚动 CollectionView 应该在右端打开另一个视图控制器和相同的功能 - Scrolling CollectionView to left after ending should open another View Controller & same functionality on right end 集合视图中的图像在滚动时会发生变化 - Images in the collectionView are changing while scrolling
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM