确定屏幕上水平 CollectionView 单元格的可见性百分比 [英] Determine Percentage of Visibility of Horizontal CollectionView Cells on Screen

查看:25
本文介绍了确定屏幕上水平 CollectionView 单元格的可见性百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个水平的 collectionView.总会有 2 个并排的单元格,它们都具有相同的宽度和高度.单元格播放视频,滚动时我想加载屏幕上 70% 的下一个单元格的视频,并停止小于该单元格的视频.我发现了几个类似的问题,但似乎所有内容都引用了 1 个单元格.

I have a horizontal collectionView. There will always be 2 cells side by side, they both have the same width and height. The cells plays videos and when scrolling I want to load the video for the next cell that is 70% on screen and stop the video for the cell that is less than that. I found several similar questions but everything seems to reference 1 cell.

1- 单元格 59 和 60 都 100% 在屏幕上

1- Cells 59 and 60 are both 100% on screen

2- 向左滚动,屏幕上 59 仍大于 70%,60 为 100%,61 小于 30%

2- Scrolling left, 59 is still more than 70%, 60 is 100%, and 61 is less than 30% on screen

3- 仍在向左滚动,59 小于屏幕上的 30%,60 是 100%,而 61 大于 70%

3- Still scrolling left, 59 is less than 30% on screen, 60 is 100%, and 61 is more than 70%

前两张照片中的单元格 59 和所有三张照片中的 60 中已经可见的任何内容,我有逻辑防止单元格再次加载,即使 cell.loadNewVideo() 会为他们奔跑.

Anything that is is already visible as in cell 59 in the first two photos and 60 in all three photos, I have logic to prevent the cell from loading again even though cell.loadNewVideo() will run for them.

layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let width = UIScreen.main.bounds.width / 2
    return CGSize(width: width, height: 150)
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {

    collectionView.visibleCells.forEach { cell in
        guard let cell = cell as? VideoCell else { continue }

        guard let indexPath = collectionView.indexPath(for: cell) else { return }
        guard let layoutAttributes = collectionView.layoutAttributesForItem(at: indexPath) else { return }
        let cellFrameInSuperview = collectionView.convert(layoutAttributes.frame, to: collectionView.superview)
        guard let superView = collectionView.superview else { return }
        let convertedRect = collectionView.convert(cellFrameInSuperview, to: superView)
        let intersect = collectionView.frame.intersection(convertedRect)

        if intersect.width > (UIScreen.main.bounds.width / 2) * 0.7 {

            cell.loadNewVideo()

        } else {

            cell.destroyOldVideo()
        }
    }
}

我也在 scrollViewWillEndDragging 中尝试了相同的代码,但它也不起作用:

I also tried the same code scrollViewWillEndDragging in but it also didn't work:

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

}

推荐答案

方式想得太多了.忘掉 layoutAttributes 和所有花哨的东西,做你想做的事:找出单元格有多少在屏幕上.

You are way overthinking this. Forget about layoutAttributes and all that fancy-pants stuff and just do what you said you wanted to do: find out how much of the cell is onscreen.

单元格是一个视图.屏幕(比方说,由窗口表示)是一个视图.与他们相交!在适当转换的坐标中查看单元格框架与窗口(或其他任何东西)的交集,以获取屏幕上单元格的部分,并将其大小与单元格框架的大小进行比较.那是你的百分比.现在做任何你喜欢这个百分比的事情.

The cell is a view. The screen (represented, let's say, by the window) is a view. Intersect them! Look at the intersection of the cell's frame with the window (or whatever), in appropriately converted coordinates, to get the part of the cell that is onscreen, and compare its size with the cell's frame's size. That is your percentage. Now do anything you like that percentage.

在这个例子中,任何你喜欢的"只是为了显示百分比.

In this example, "anything you like" is just to display the percentage.

override func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let cells = self.collectionView.visibleCells
    for cell in cells {
        let f = cell.frame
        let w = self.view.window!
        let rect = w.convert(f, from: cell.superview!)
        let inter = rect.intersection(w.bounds)
        let ratio = (inter.width * inter.height) / (f.width * f.height)
        let rep = (String(Int(ratio * 100)) + "%")
        let label = cell.contentView.subviews.first as! UILabel
        label.text = rep
    }
}

这篇关于确定屏幕上水平 CollectionView 单元格的可见性百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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