确定表格视图单元格中的图像是否完全可见 [英] Determine if image in table view cell is completely visible

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

问题描述

我有一个表格视图,其中每个表格视图单元格都有一个图像.

I have a table view where each table view cell has an image.

我需要确定图像视图何时在滚动时完全可见.此外,如果两个单独的表格视图单元格中的两个图像同时完全可见,我只想触发第一个图像的操作.

I need to determine when an image view is completely visible on scroll. Additionally, if two images in two separate table view cells are completely visible at the same time, I want to only trigger an action for the first image.

我想过使用 willDisplay,但我发现这只会触发一次,并且在单元格出现后立即触发.

I've thought of using willDisplay, but I've found that this only ever triggers once and it triggers as soon as the cell comes into view.

我也发现了这个问题,但是它特定于确定表格视图单元格本身是否完全可见,而不是表格视图单元格内的视图.

I also found this question, but it's specific to determining if the table view cell itself is completely visible, rather than a view within a table view cell.

我有哪些选择?解决这个问题的最佳方法是什么?

What are my options? What is the best way of going around this?

推荐答案

假设您已经设置了管理图像单元格的视图控制器,这里是该控制器的一部分代码,即 UITableView,演示了一种如何跟踪图像可见的方法.希望能帮到你.

Assuming you have set up view controller that manages image cells, here is a part of code for such controller, which is delegate of UITableView, that demos an approach of how to track becoming images visible. Hope it will be helpful.

@IBOutlet weak var tableView: UITableView!

override func viewDidAppear(_ animated: Bool) {
    self.verifyImagesVisibility()
}

func scrollViewDidScroll(_ scrollView: UIScrollView)  {
    self.verifyImagesVisibility()
}

private func verifyImagesVisibility() {
    for cell in self.tableView.visibleCells {
        if let imageView = cell.imageView {
            let visibleRect = self.tableView.bounds
            let imageRect = imageView.convert(imageView.bounds, to: tableView)

            imageView.layer.borderWidth = 4.0 // for test
            if visibleRect.contains(imageRect) {
                // do anything here for image completely shown
                imageView.layer.borderColor = UIColor.red.cgColor // for test
            } else {
                // do anything here for image become partially hidden
                imageView.layer.borderColor = UIColor.black.cgColor // for test
            }
        }
    }
}

这篇关于确定表格视图单元格中的图像是否完全可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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