【Swift】自定义UITableView可拉伸的HeadView

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013466880/article/details/79978708

效果展示

b38076f0-4f11-49df-a23a-61af365d062e.gif

代码实现

import UIKit

class RKSpringTableHeadView: UIView {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    var kImageViewHeight: CGFloat = 0.0

    var imageString: String? {
        didSet {
            imageView.image = UIImage.init(named: imageString!)
        }
    }

    var imageHeight: CGFloat? {
        didSet {
            kImageViewHeight = imageHeight!
            imageView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: imageHeight!)
        }
    }

    lazy var imageView: UIImageView = {

        let imageView = UIImageView()
        imageView.contentMode = .scaleToFill
        imageView.clipsToBounds = true
        self.addSubview(imageView)
        self.addSubview(sloganView)

        return imageView
    }()

    lazy var sloganView:UIImageView = {

        let imageView = UIImageView(image: UIImage.init(named: "bannerSlogan_Normal"))
        imageView.frame = CGRect(x: 0, y: (imageHeight! - 36) * RKConfig.kScreenScale, width: self.frame.size.width, height: (28 * RKConfig.kScreenScale))
        imageView.contentMode = .scaleAspectFit

        return imageView
    }()

    func scrollViewDidScroll(scrollView: UIScrollView) -> Void {

        let imageWidth = self.frame.size.width
        let imageOffsetY = scrollView.contentOffset.y
//        print("图片上下偏移量 imageOffsetY: \(imageOffsetY)")

        //上移
        if (imageOffsetY < 0) {

            var frame = CGRect()
            frame.origin.y = imageOffsetY
            frame.origin.x = imageOffsetY/2.0
            frame.size.width = imageWidth - imageOffsetY
            frame.size.height = (kImageViewHeight - imageOffsetY)

            imageView.frame = frame
        }
    }
}

extension UITableView {

    func rk_addSpringHeadView(view: RKSpringTableHeadView) -> Void {

        DispatchQueue.main.async {

            let emptyHeadView = UIView(frame: view.frame)
            self.tableHeaderView = emptyHeadView
            self.showsVerticalScrollIndicator = false

            self.addSubview(view)
        }
    }
}

代码调用

import UIKit

class RKHouseInfoViewController: RKBaseViewController, UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {

    static let kHomeHousInfoCell: String = "RKHouseInfoCell"

    lazy var tableView:UITableView = {

        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: RKHouseInfoViewController.kHomeHousInfoCell)
//        tableView.frame = self.view.frame

        return tableView
    }()

    lazy var springHeadView: RKSpringTableHeadView = {

        let headView = RKSpringTableHeadView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 128))
        headView.imageHeight = 128
        headView.imageString = "banner_Normal"

        return headView
    }()

    lazy var searchBar:RKSearchBar = {

        let searchBar = RKSearchBar()
        searchBar.areaTitle = "苏州"
        searchBar.frame = CGRect(x: 10, y: 30, width: self.view.frame.size.width - 20, height: 44 * RKConfig.kScreenScale)

        return searchBar
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        addConstraints()
    }

    //MARK: 添加视图约束
    func addConstraints() -> Void {

        self.view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(0)
            make.right.equalToSuperview().offset(0)
            make.top.equalToSuperview().offset(0)
            make.bottom.equalToSuperview().offset(0)
        }

        tableView.rk_addSpringHeadView(view: springHeadView)

        self.view.addSubview(searchBar)
    }
    //MARK: TableView Delegate

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: RKHomeViewController.kHomeHousInfoCell)
        cell?.textLabel?.text = "你好你好你好"

        return cell!
    }

    //MARK: ScrollView Delegate

    func scrollViewDidScroll(_ scrollView: UIScrollView) {

        springHeadView.scrollViewDidScroll(scrollView: scrollView)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页