Determine coordinates of a UITableViewCell while scrolling

后端 未结 4 1944
生来不讨喜
生来不讨喜 2020-12-02 21:14

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.

The approach I have bee

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-02 21:48

    The first step is to use

    CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];

    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]];
    

    So the following line does it all

    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)
    

提交回复
热议问题