How to "Pan" (swipe) a UITableView from top to bottom using UIPanGestureRecognizer , When 1st cell is at the top of the UITableView?

Viewed 1210

Scenario 1: I have a UITableView without header at the bottom of the View, with a complete display of the first cell and the UITableView Scrolling is disabled. I need to Pan up (swipe up) the UITableView to the top of the view with extending the height and when the UITableView hits the top of the view, table's header should be displayed with animation and scrolling should be enabled.

Scenario 2: The UITableView needs to be panned down(swiped down) to its initial state with scrolling disabled and without header, as it was in the starting position. The swiping should happen only if the first cell is at the top of the UITableView.

Scenario 1 solved solution: I have added UIPanGestureRecognizer to the UITabeView and panned (swiped) the UITableView to the top of the view. After that, I have enabled the table scrolling and reloading my table data. In height for a header in section, I am returning the header height only if the scrolling is enabled.

Scenario 2 tried solution: After the table comes to the top, I removed the UIPanGestureRecognizer from the UITableView and added it to the tables's header. When I drag the header view table goes down to its initial position and scrolling is disabled. But what I need is, the scrolling should happen in the UITableView itself, only if the first cell is at the top of the UITableView.

@IBOutlet public var headerView !
@IBOutlet public var tableView !
var intialTableFrame : CGRect!

// setting panGesture for table
override func viewDidLoad() {
   panGesture = UIPanGestureRecognizer(target: self, 
   action: #selector("handlePanGesture:"))
   tableView.addGestureRecognizer(panGesture )
   tableView.isScrollEnabled = false
}
// panGesture

@objc func handlePanGesture(sender: UIPanGestureRecognizer) {
   let translation = sender.translation(in: self.tableView)
//   let velocity = sender.velocity(in: self.tableView)

   let oldFrame = self.tableView.frame
   if sender.state == UIGestureRecognizer.State.changed {
      self.tableView.frame.origin.y = oldFrame.origin.y + translation.y
      self.tableView.frame.size.height = oldFrame.size.height - translation.y
      sender.setTranslation(CGPointZero, inView: self.tableView)
   } 
   else if sender.state == UIGestureRecognizer.State.ended {
      if self.tableView.frame.origin.y < self.view.center.y {
         UIView.animate(withDuration:0.3, animations: { () -> Void in
            self.tableView.frame.origin.y = 0
            self.tableView.frame.size.height = self.view.frame.size.height
         })
         self.tableView.isScrollEnabled = true
         self.tableView.reloadData() // This will add headerView to the table
         self.tableView.removeGestureRecognizer(panGesture)
         self.headerView.addGestureRecognizer(panGesture )   
      } 
      else {
          UIView.animate(withDuration:0.3, animations: { () -> Void in
            self.tableView.frame = self.intialTableFrame 
         })  
         self.tableView.isScrollEnabled = false
         self.tableView.reloadData() // This will remove headerView to the table
         self.headerView.removeGestureRecognizer(panGesture)
         self.tableView.addGestureRecognizer(panGesture ) 
      }
   }
}

Expected Results : When i swipe the UITableView from top to bottom, the table should panned down only if the 1st cell is at the top of the UITableView. Otherwise the table should scroll as normal with the same position
0 Answers
Related