[英]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
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.