如何在Swift 3中多次触摸UICollectionViewCells

时间:2016-10-15 17:31:09

标签: ios swift uicollectionview uicollectionviewcell uitapgesturerecognizer

对于当前项目,我需要能够同时触摸多个UICollectionViewCell。 我试图使用UICollectionViewDelegate,但我仍然坚持能够同时触摸Cell。

这是我的代码:

import UIKit

private let reuseIdentifier = "CustomCell"

class CollectionViewController: UICollectionViewController {

let sectionInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
let numOfPads    = 24
let numOfColumns = 4

func initConfig(){
    collectionView?.backgroundColor = UIColor.white
    //
    collectionView?.indicatorStyle = UIScrollViewIndicatorStyle.white
    //
    //collectionView?.backgroundView = UIView(frame: (collectionView?.frame)!)
    // lets iOS recover and respond to more than one finger touch at a time
    collectionView?.isMultipleTouchEnabled = true
    collectionView?.allowsSelection = true
    collectionView?.allowsMultipleSelection = true
    collectionView?.isUserInteractionEnabled = true

    // This makes tapping on the UICollectionViewCell feel zippy
    collectionView?.delaysContentTouches = false
    // Kill any kind of scrolling
    collectionView?.isScrollEnabled = false
}

func generateRandomColor() -> UIColor {
    let redValue = CGFloat(arc4random() % 255) / 255.0
    let blueValue = CGFloat(arc4random() % 255) / 255.0
    let greenValue = CGFloat(arc4random() % 255) / 255.0

    return UIColor(red: redValue, green: greenValue, blue: blueValue, alpha: 0.5)
}

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Register cell classes
    self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    // Do any additional setup after loading the view.
    initConfig()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: UICollectionViewDataSource

override func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 24
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)

    cell.backgroundColor = generateRandomColor()

    return cell
}


// MARK: UICollectionViewDelegate

override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
    if let selectedItems = collectionView.indexPathsForSelectedItems {
        if selectedItems.contains(indexPath as IndexPath) {
            collectionView.deselectItem(at: indexPath as IndexPath, animated: true)
            return false
        }
    }
    return true   
}

override func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
    print("didHighlightItemAt", indexPath)
    let cell = collectionView.cellForItem(at: indexPath)! as UICollectionViewCell
        cell.contentView.backgroundColor = UIColor.white
}

override func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
    //print("didUnhighlightItemAt", indexPath)
    let cell = collectionView.cellForItem(at: indexPath)! as UICollectionViewCell
    cell.contentView.backgroundColor = UIColor.clear
}

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("didSelectItemAt", selectedCells)

}
}

1 个答案:

答案 0 :(得分:1)

要“激活”多个单元格选择,您只需使用shouldSelectItemAt的{​​{1}}函数来允许它。

但在你的情况下,我们需要激活多个Cell touch。为了能够这样做,我现在使用了UIGestureRecognizers:

首先,我们需要为每个Cell设置一个UICollectionViewDelegate

GestureRecognizer

然后我们需要接收被点击的Cell:

extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate {

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath)

        let lpgr = UITapGestureRecognizer(target: self, action: #selector(tap(gestureReconizer:)))
        lpgr.delegate = self
        cell.addGestureRecognizer(lpgr)

        return cell
    }
}

现在我们可以同时触摸细胞。