Want UITableView to “snap to cell”

前端 未结 7 1617
长发绾君心
长发绾君心 2020-12-23 17:40

I am displaying fairly large images in a UITableView. As the user scrolls, I\'d like to the table view to always snap the center-most photo in the middle. That

7条回答
  •  鱼传尺愫
    2020-12-23 18:06

    for the swift peeps

    override func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) 
    {
       if decelerate == false
       {
           self.centerTable()
       }
     }
    
    override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        self.centerTable()
    }
    
    func centerTable()
    {
       let midX:CGFloat = self.tableView.bounds.midX
       let midY:CGFloat = self.tableView.bounds.midY
       let midPoint:CGPoint = CGPoint(x: midX, y: midY)
    
       if let pathForCenterCell:IndexPath = self.tableView .indexPathForRow(at: midPoint)
        {
          self.tableView.scrollToRow(at: pathForCenterCell, at: .middle, animated: true)
        }
    }//eom
    

提交回复
热议问题