首页 > 解决方案 > UIcollectionview 滚动时调用 func(didSelectItemAt)

问题描述

目前我正在使用UICollectionView其中的几个单元格。 UICollectionView水平滚动以在单元格之间移动。
我的问题是:有没有办法通过在滚动UICollectionView单击单元格来调用 func(didSelectItemAt) ?

这是gif。

在将单元格向左拖动以进行滚动后,我连续单击单元格以调用 func(didSelectItemAt) 但仅在滚动完成时才会调用。是否可以在滚动时单击单元格后立即调用 func(didSelectItemAt) ?

这是我的 collectionView 委托和数据源代码。

extension CardViewController : UICollectionViewDelegate,UICollectionViewDataSource {
public func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = cardCollectionView.dequeueReusableCell(withReuseIdentifier: "card", for: indexPath) as! CardCell
    cell.isFront = cellsSelectedStatus[indexPath.row]
    return cell
}


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let cell = cardCollectionView.cellForItem(at: indexPath) as! CardCell
    UIView.transition(with: cell, duration: 0.3, options: [.transitionFlipFromLeft,.allowUserInteraction], animations: nil, completion: nil)
    cellsSelectedStatus[indexPath.row] = !(cellsSelectedStatus[indexPath.row])
    cell.isFront = cellsSelectedStatus[indexPath.row]

    print("did select")

}

Collectionview 的 dataSource&delegate 在viewDidLoad.

我知道有一个允许在动画时单击视图allowUserInteractionUIViewAnimationOptionsfunc(didSelectItemAt) 是否有更简洁的代码?

没有网络和 UIGestureRecognizers 附加到单元格或集合视图。到目前为止,我已将 collectionView 的 , , 设置 isUserInteractionEnabledisMultipleTouchEnabledtrue allowsMultipleSelection,但它的行为仍然相同。我正在使用 Swift3、Xcode9。

提前致谢。

标签: swiftscrolluicollectionviewuicollectionviewcell

解决方案


推荐阅读