我有一个collectionView
垂直滚动,覆盖设备上的整个屏幕(即全屏)。
我已经注册了Swipe Left and Right
为我的手势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:左右滑动手势回调不会触发collectionView
正在垂直滚动。
有没有任何简单的解决方法。
这是我的全部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.
}
}
这是我的collectionView
看起来像
EDIT 1
已解决,解决方案请点击here https://stackoverflow.com/questions/38346679/swipe-left-right-on-collectionview-not-called-while-scrolling-vertically/38369367#38369367