检查单元格是否完全显示在集合视图中 [英] Check if cell completely displayed in collectionview

查看:23
本文介绍了检查单元格是否完全显示在集合视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集合视图,其中单元格的大小与集合视图完全相同,因此每个单元格都应占据整个屏幕.我已经实现了一个功能,当它在滚动中拖动或减速时,单元格会被捕捉到完整的视图.这就是用户体验的工作方式.

I have a collection view where the cell is of the size exactly to the collectionView, so each cell should occupy the whole screen. I have implemented a functionality where the cell is snapped to the complete view whenever it's dragged or decelerated through the scroll. This is how the UX works.

https://drive.google.com/open?id=1v8-WxCQUzfu8V_k9zM1UCWsf_-Zz4dpr

我想要的:

正如您从剪辑中看到的,单元格对齐整个屏幕.现在,我想在它捕捉后执行一个方法.在部分显示之前或之前不显示.

As you can see from the clip, the cell snaps to the whole screen. Now, I want to execute a method after it snaps. Not before or not when it's partially displayed.

以下是我为捕捉效果编写的代码:

Following is the code I have written for snapping effect :

func scrollToMostVisibleCell(){
    let visibleRect = CGRect(origin: collectionView.contentOffset, size: collectionView.bounds.size)
    let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
    let visibleIndexPath = collectionView.indexPathForItem(at: visiblePoint)!
    collectionView.scrollToItem(at: visibleIndexPath as IndexPath, at: .top, animated: true)
    print("cell is ---> ", visibleIndexPath.row)
}



func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    scrollToMostVisibleCell()
}

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    scrollToMostVisibleCell()
    if !decelerate {
        scrollToMostVisibleCell()
    }
}

如果我使用 willDisplayCell 方法,那么只要单元格在视图中,它就会立即返回我,即使它只是在 collectionView 中偷看.

If I use willDisplayCell method, then it' just going to return me as soon as the cell is in the view, even if it's just peeping in the collectionView.

有没有办法检查单元格是否完全在视图中,然后我可以执行某个功能?

我已经为这个问题在互联网上报废了,但找不到满意的答案.

I have scrapped the internet over this question, but ain't able to find a satisfactory answer.

推荐答案

使用 followedCollectionView.indexPathsForVisibleItems() 获取可见单元格 visibleIndexPaths 并检查您的 indexPath<在对单元格执行任何操作之前,/code> 是否包含在 visibleIndexPaths 中.参考:@anhtu 检查 indexPath 处的单元格是否可见屏幕 UICollectionView

Using followedCollectionView.indexPathsForVisibleItems() to get visible cells visibleIndexPaths and check your indexPath is contained in visibleIndexPaths or not, before doing anything with cells. Ref : @anhtu Check whether cell at indexPath is visible on screen UICollectionView

同样来自 Apple :var visibleCells: [UICollectionViewCell] { get } .返回集合视图当前显示的可见单元格数组.

Also from Apple : var visibleCells: [UICollectionViewCell] { get } . Returns an array of visible cells currently displayed by the collection view.

这篇关于检查单元格是否完全显示在集合视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆