I used to do Object-C before, but recently I learned Swift, and now I use Swift to achieve the effect.
The first is to use UITableView as the theme frame,
1. Create a .swift file (similar to the PCH file in OC), write import UIKit in the header to create a variable SCREEN to store the Rect of the screen
let SCREEN = UIScreen.main.bounds;
2. Create the top picture and View
//The top picture and View
lazy var headerImageViewTop:UIImageView = {
let header = UIImageView.init(frame: CGRect(x:0,y:0,width:SCREEN.width,height:160))
header.backgroundColor = UIColor.red
header.image = UIImage.init(named: "timg.jpeg")
header.contentMode = UIViewContentMode.scaleAspectFill
header.clipsToBounds = true
return header
}()
lazy var headerBackView:UIView = {
let headerView = UIView.init(frame: CGRect(x:0,y:0,width:SCREEN.width,height:160))
headerView.backgroundColor = UIColor.lightGray
return headerView
}()
3.UITableView
//MARK:- interface element
func createView() -> Void {
myTableView.frame = CGRect(x:0,y:0,width:SCREEN.width,height:SCREEN.height-49-64)
myTableView.delegate = self
myTableView.dataSource = self
myTableView.backgroundColor = UIColor .white
let nib = UINib.init(nibName: "TMVCableViewCell", bundle: nil)
myTableView.register(nib, forCellReuseIdentifier: "Cell")
myTableView.tableHeaderView = self.headerBackView
self.headerBackView.addSubview(self.headerImageViewTop)
}
4. Set the coordinate change when scrolling in the proxy
//MARK:- after scrolling tableView
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let imageWeight:CGFloat = headerImageViewTop.frame.size.width
//up and down offset
let imageOffsetY:CGFloat = scrollView.contentOffset.y
//Move up
if imageOffsetY<0 {
let totalOffset:CGFloat = 160+abs(imageOffsetY)
if abs(imageOffsetY)>160 {
return
}
self.headerImageViewTop.frame = CGRect(x:0,y:imageOffsetY,width: imageWeight,height:totalOffset)
}
}