滚动时确定 UITableViewCell 的坐标 [英] Determine coordinates of a UITableViewCell while scrolling

查看:28
本文介绍了滚动时确定 UITableViewCell 的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是让 UITableViewCells 在接近 UITableView 的边界并即将被覆盖/显示时淡入/淡出.

My goal is to have the UITableViewCells fade in/out when they are approaching the bounds of the UITableView and about to be covered/revealed.

我一直在尝试的方法是在滚动事件期间获取 UITableViewCell 的坐标.问题是每个单元格似乎都在 0,0 处.我尝试将坐标转换为父表和视图,但它们仍然以 0,0 出现.

The approach I have been trying is to get the coordinates of the UITableViewCell during a scroll event. The problem is that every cell seems to be at 0,0. I have tried converting the coordinates to the parent table and view, but they still come out at 0,0.

因此,总的来说,如果有人知道获取坐标的方法,或者知道根据位置使 UITableViewCells 淡入淡出的更好方法,我将不胜感激.

So in general, if anyone knows a way to get the coordinates, or of a better way to go about fading UITableViewCells in and out based on their position, I would greatly appreciate any advice you may have.

感谢您的时间,乔尔

推荐答案

第一步就是使用

CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];

它将报告 tableView 中单元格的 CGRect.但是,这个值不会随着 tableView 滚动而改变.它是相对于表格第一行(而不是第一个可见行)的位置.要获得单元格在屏幕上的位置,您必须使用

which will report the CGRect of a cell within the tableView. However, this value does not change as the tableView scrolls. It is the position relative to the first row of the table (and not the first visible row). To get the position of the cell on the screen you have to convert it to the superviews coordinate system using

CGRect rect = [tableView convertRect:rectInTableView toView:[tableView superview]];

所以下面这行就完成了

CGRect rect = [tableView convertRect:[tableView rectForRowAtIndexPath:indexPath] toView:[tableView superview]];

Swift 4

// Consider the indexPath at row 1, section 0.
let rectWithinTableView : CGRect = self.tableView.rectForRow(at: IndexPath(row: 1, section: 0))
let rectWithSuperViewCoordinates : CGRect = self.convert(rect: rectWithinTableView, to: self.tableView.superview)

这篇关于滚动时确定 UITableViewCell 的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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