简体   繁体   English

一旦ios迅速滚动结束,如何在选择中心单元处触发didSelectItemAtIndexPath?

[英]How to trigger didSelectItemAtIndexPath at select center cell once the scroll end in ios swift?

Here UPCarouselFlowLayout is used for carousel scroll. 这里UPCarouselFlowLayout用于轮播滚动。 As of now, user must tap a cell in order to trigger collection view didSelectItemAtIndexPath. 到目前为止,用户必须点按一个单元格才能触发集合视图didSelectItemAtIndexPath。 Is there a way to select the center cell once the scrolling ended automatically? 滚动自动结束后,是否可以选择中心单元格?

here is the code i used to carousel: 这是我用于轮播的代码:

 let layout = UPCarouselFlowLayout()
    layout.itemSize = CGSize(width: 211, height: 75)
    layout.scrollDirection = .horizontal
    layout.spacingMode = UPCarouselFlowLayoutSpacingMode.fixed(spacing: 10)
    layout.spacingMode = UPCarouselFlowLayoutSpacingMode.overlap(visibleOffset: 65)
    carCollection.collectionViewLayout = layout

here the code used for collection view: 这是用于集合视图的代码:

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


    return carCategory.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! carCollectionViewCell

    cell.carName.text = carCategory[indexPath.row]
    cell.carImage.image = UIImage(named: carCategoryImage[indexPath.row])
    cell.carMeters.text = carCategoryMeter[indexPath.row]

    return cell

}

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

        print("selected index::\(indexPath.row)")

}

If you look at ViewController.swift from the Demo included with ** UPCarouselFlowLayout**, you will see the function scrollViewDidEndDecelerating . 如果查看** UPCarouselFlowLayout **随附的Demo中的ViewController.swift ,您将看到函数scrollViewDidEndDecelerating That is triggered when the scroll stops moving and a cell become the "center" cell. 当滚动停止移动并且某个单元格成为“中心”单元格时,将触发该事件。

In that function, the variable currentPage is set, and that's where the labels below the collection view are changed. 在该函数中,设置了变量currentPage ,这就是更改集合视图下方的标签的地方。

So, that's one place to try what you want to do. 所以,这是一个地方试试你想要做什么。

Add the two lines as shown here... when the scroll stops, you create an IndexPath and manually call didSelectItemAt : 如下图所示添加两行...当滚动停止时,创建一个IndexPath并手动调用didSelectItemAt

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    let layout = self.collectionView.collectionViewLayout as! UPCarouselFlowLayout
    let pageSide = (layout.scrollDirection == .horizontal) ? self.pageSize.width : self.pageSize.height
    let offset = (layout.scrollDirection == .horizontal) ? scrollView.contentOffset.x : scrollView.contentOffset.y
    currentPage = Int(floor((offset - pageSide / 2) / pageSide) + 1)

    // add these two lines      
    let indexPath = IndexPath(item: currentPage, section: 0)
    collectionView(self.collectionView, didSelectItemAt: indexPath)
}

You will almost certainly want to add some error checking and additional functionality (like only calling didSelect if the cell actually changed, as opposed to just sliding it a little but remaining on the current cell), but this is a starting point. 您几乎肯定会想要添加一些错误检查和其他功能(例如,如果单元格实际上发生了更改,则仅调用didSelect,而不是稍微滑动一下但保留在当前单元格上),但这是一个起点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果滚动,swift collectionview单元格didSelectItemAtIndexPath显示错误的单元格 - swift collectionview cell didSelectItemAtIndexPath shows wrong cell if you scroll didSelectItemAtIndexPath没有在ios swift中调用 - didSelectItemAtIndexPath is not calling in ios swift iOS:如何选择并滚动到swift 3中collectionView中的特定单元格? - iOS: How to select and scroll to a specific cell in a collectionView in swift 3? IOS如何从cell.contentview.subview [0]的collectionView单元格didSelectItemAtIndexPath方法获取图像 - ios how to get image from collectionView cell didSelectItemAtIndexPath method from cell.contentview.subview[0] iOS-有没有办法选择一个单元格但不能滚动到它? - iOS - Is there a way to select a cell but not scroll to it? 如何避免在TableView iOS Swift中滚动时刷新每个单元格数据? - how to avoid refresh each cell data on scroll in tableview ios swift? UITableView Cell不应该横向滚动swift iOS - UITableView Cell should not scroll horizontally swift iOS 在 swift 中收集视图单元格滚动到中心时出现问题 - Issue while collection view cell scroll to center in swift Swift iOS MetalKit绘制一条曲线,为什么曲线的末端始终在中心,我该如何修复? - Swift iOS MetalKit draw a curve , why the end of the curve is always in the center, how can i fix it? Swift Collectionview didSelectItemAtIndexPath不起作用 - Swift Collectionview didSelectItemAtIndexPath not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM