table的HeaderView是一张图片,跟着table的滑动变大变小
只需要继承下面控制器即可
下载连接:https://download.csdn.net/download/qq_37191821/11942670
import UIKit
/// 屏幕的宽度
private let screenW: CGFloat = UIScreen.main.bounds.width;
/// 头部视图的高度
private let headerviewH: CGFloat = screenW*0.6;
/// 导航栏的高度
private let navigationH: CGFloat = UINavigationController().navigationBar.frame.size.height;
/// 状态栏的高度
private let statusH: CGFloat = UIApplication.shared.statusBarFrame.size.height;
/// 深绿色
private let darkGreen = UIColor(hue:0.40, saturation:0.78, brightness:0.68, alpha:1.00)
class ZJTableController: UIViewController,UITableViewDelegate{
public var table : UITableView = {
let size = UIScreen.main.bounds.size;
let view = UITableView(frame: CGRect(x: 0, y: 0, width: size.width, height: size.height), style: UITableViewStyle.plain);
// 设置表格顶部间距, 使得HaderView不被遮挡
view.contentInset = UIEdgeInsets(top: headerviewH, left: 0, bottom: 0, right: 0)
// 设置指示器的间距, 等于表格顶部的间距
view.scrollIndicatorInsets = view.contentInset;
// 注册cell
view.register(UITableViewCell.self, forCellReuseIdentifier: "CellID");
return view;
}();
public var HeaderImage : UIImageView = {
let view = UIImageView(frame: CGRect(x: 0, y: 0, width: screenW, height: headerviewH));
// 设置图像
view.image = UIImage(named: "Girl1")
// 设置图像显示比例
view.contentMode = .scaleAspectFill
// 设置图像裁切
view.clipsToBounds = true
//view.backgroundColor = UIColor.red;
return view;
}()
///MARK:Headerview的高度是否固定
private var HeaderviewIsFixed = false;
private var Headerview : UIView = {
let view = UIView(frame: CGRect(x: 0, y: 0, width: screenW, height: navigationH+statusH));
view.backgroundColor = UIColor(hue:0.40, saturation:0.78, brightness:0.68, alpha:1.00);
view.alpha = 0;
return view;
}()
private var lineView : UIView = {
let lineH = 1 / UIScreen.main.scale
let view = UIView(frame: CGRect(x: 0, y: headerviewH - lineH, width: screenW, height: lineH))
view.backgroundColor = UIColor.lightGray;
return view;
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.orange;
// Do any additional setup after loading the view.
// 隐藏导航栏, 带动画
navigationController?.setNavigationBarHidden(true, animated: true)
// iOS7以后, 导航控制器中ScrollView\tableView顶部会添加 64 的额外高度 取消自动调整滚动视图间距
automaticallyAdjustsScrollViewInsets = false
view.addSubview(table);
view.addSubview(HeaderImage);
Headerview.addSubview(lineView);
view.addSubview(Headerview);
table.delegate = self;
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let offsetY = scrollView.contentOffset.y + scrollView.contentInset.top
guard offsetY <= 0 else {// 上滑
// MARK: 整体移动
HeaderImage.frame.size.height = headerviewH
HeaderImage.frame.origin.y = -offsetY
/// HeaderView最小的Y值
let headerViewMinY = CGFloat(headerviewH - navigationH - statusH) // 显示导航栏
//let headerViewMinY = headerviewH - statusH // 显示状态栏
// min函数: 取最小值
HeaderImage.frame.origin.y = -min(headerViewMinY, offsetY)
// 设置透明度
// 根据输出, 得知当 offsetY / headerViewMinY == 1时,不可见图像
let progress = (offsetY / headerViewMinY)
Headerview.alpha = progress
if HeaderviewIsFixed == false {
Headerview.frame = HeaderImage.frame;
}
return
}
Headerview.alpha = 0
// MARK: 放大图像
// 调整HeaderView\ HeaderImage
HeaderImage.frame.origin.y = 0
// 增大 Headerview 高度
HeaderImage.frame.size.height = headerviewH - offsetY
// 设置分割线的位置
lineView.frame.origin.y = Headerview.frame.size.height - lineView.frame.size.height
// 图像视图高度 = headView高度
//HeaderImage.frame.size.height = Headerview.frame.size.height
}
}