When the TableView drops down, the head height changes. As shown in the figure.
1.gif
1, add a view to tableView, set the tableView contentInset, so that the part of the tableView is empty, just display the view. 2. In scrollViewDidScroll, set the height and y of the headerView according to the sliding distance. Since we need to fix the HeaderView at the top, we need to adjust the y value as well.
There are many zoom effects of pull-down images. You can change the scale Transform according to the pull-down distance.
Note that after changing contentInset, contentOffset changes, which in fact changes the bounds of the TableView. Assuming contentinset. top = 20, contentOffset = -20, bounds.y = -20. Changes in bounds affect the layout of the subview. By default, bounds.origin = CGPointZero, where (0, 0) is in the upper left corner, and if we change bounds.y = 20, the (0, 20) coordinate is displayed in the upper left corner, which is equivalent to moving 20 up.
The main code is as follows:
Create a header
var headerView: UIView? let headerViewHeight: CGFloat = 44 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. tableView.contentInset = UIEdgeInsetsMake(headerViewHeight, 0, 0, 0) headerView = UIView() .frame = CGRectMake(0, -headerViewHeight, view.bounds.size.width, headerViewHeight) headerView! .backgroundColor = UIColor.redColor() tableView.addSubview(headerView!) }Copy the code
Listen to scroll
func scrollViewDidScroll(scrollView: UIScrollView) {// Set contentinset.top, The contentOffset. Y = - contentInset. Top - realOffset let contentOffsetY = scrollView. ContentOffset. Y / / real scroll offset let totalOffsetY = scrollView.contentInset.top + contentOffsetY print("contentOffsetY:\(contentOffsetY), totalOffsetY:\(totalOffsetY)") if let headerView = headerView { var size = headerView.frame.size size.height = headerViewHeight - totalOffsetY headerView.frame = CGRectMake(0, contentOffsetY, size.width, size.height) } }Copy the code