簡體   English   中英

確定表格視圖單元格中的圖像是否完全可見

[英]Determine if image in table view cell is completely visible

我有一個表格視圖,其中每個表格視圖單元格都有一個圖像。

我需要確定圖像視圖何時在滾動時完全可見。 此外,如果兩個單獨的表格視圖單元格中的兩個圖像同時完全可見,我只想觸發第一個圖像的操作。

我曾想過使用willDisplay ,但我發現這只會觸發一次,並且一旦單元格進入視野就會觸發。

我也發現了這個問題,但它特定於確定表視圖單元格本身是否完全可見,而不是表視圖單元格中的視圖。

我有哪些選擇? 解決這個問題的最佳方法是什么?

假設您已經設置了管理圖像單元格的視圖控制器,這里是這種控制器的一部分代碼,它是UITableView委托,它演示了一種如何跟蹤圖像可見的方法。 希望它會有所幫助。

@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
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM