垂直滚动时未调用 collectionView 上的向左/向右滑动 [英] Swipe Left/Right on collectionView not called while scrolling vertically

查看:32
本文介绍了垂直滚动时未调用 collectionView 上的向左/向右滑动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有垂直滚动功能的 collectionView,覆盖设备上的整个屏幕(即全屏).

我已经为我的 collectionView 注册了 左右滑动 手势.

//------------collectionView 中的向右滑动手势--------------//让 swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.rightSwiped))swipeRight.direction = UISwipeGestureRecognizerDirection.Rightself.collectionView.addGestureRecognizer(swipeRight)//-----------collectionView 中的向左滑动手势--------------//让 swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.leftSwiped))swipeLeft.direction = UISwipeGestureRecognizerDirection.Leftself.collectionView.addGestureRecognizer(swipeLeft)

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

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

这是我的整个 ViewController

导入 UIKit类视图控制器:UIViewController、UICollectionViewDelegate、UICollectionViewDataSource {@IBOutlet 弱变量 collectionView:UICollectionView!letuseuseIdentifier = "cell"//也输入这个字符串作为故事板中的单元标识符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"]覆盖 func viewDidLoad() {super.viewDidLoad()//在加载视图后做任何额外的设置,通常是从笔尖.collectionView.dataSource = selfcollectionView.delegate = self//------------collectionView 中的向右滑动手势--------------//让 swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.rightSwiped))swipeRight.direction = UISwipeGestureRecognizerDirection.Rightself.collectionView.addGestureRecognizer(swipeRight)//-----------collectionView 中的向左滑动手势--------------//让 swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.leftSwiped))swipeLeft.direction = UISwipeGestureRecognizerDirection.Leftself.collectionView.addGestureRecognizer(swipeLeft)}func collectionView(collectionView: UICollectionView, numberOfItemsInSection 部分: Int) ->整数{返回 self.items.count}func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) ->UICollectionViewCell {//获取对我们的故事板单元格的引用让 cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as!自定义单元格//使用我们自定义类中的 outlet 来获取对单元格中 UILabel 的引用cell.lable.text = self.items[indexPath.item]cell.backgroundColor = UIColor.yellowColor()返回单元格}func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {打印(indexPath.row)}func rightSwiped(){打印(向右滑动")}func leftSwiped(){打印(向左滑动")}覆盖 func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()//处理任何可以重新创建的资源.}}

这是我的 collectionView 看起来像

编辑 1

已解决,请点击这里

解决方案

UIColllcetionView 的默认手势识别器的 delegate 是集合视图对象本身(显然).

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer的默认实现在UICollection类中返回YES.>

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

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

这应该使您的 rightSwiped()leftSwiped() 在发生相应的滑动时被触发.

I have a collectionView with vertical scrolling, covering whole screen on the device (i.e fullscreen).

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

Problem: Swipe left and right gestures callback does not fire while collectionView is scrolling vertically.

Is there any simple workaround for this.

here is my whole 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

EDIT 1

Solved, for solutions click here

解决方案

The delegate of default gesture recognisers of the UICollcetionView is collection view object itself (obviously).

The default implementation of -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizerreturns YESin side the UICollectionView class.

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.

这篇关于垂直滚动时未调用 collectionView 上的向左/向右滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆