gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 22:12:45 32 4
gpt4 key购买 nike

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

我已经为我的 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

最佳答案

UICollcetionView 的默认手势识别器的委托(delegate) 是 Collection View 对象本身(显然)。

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

因此,为了解决您的问题,您需要将 Collection View 对象设置为“左”和“右”滑动手势识别器的委托(delegate),如下所示。

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

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

关于ios - 垂直滚动时在未调用的 collectionView 上向左/向右滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38346679/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com