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