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

效果展示

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

代碼實(shí)現(xiàn)

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)
        }
    }
}

代碼調(diào)用

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.
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,816評(píng)論 25 709
  • 很久很久以前,我認(rèn)識(shí)了一個(gè)女生,我們一開(kāi)始并不熟悉,直到我們住在一個(gè)小區(qū),然后就在小區(qū)遇見(jiàn)了,又然后就一起走啊走,...
    CheenJSheng閱讀 290評(píng)論 0 0
  • 《康熙王朝》看了很多遍,每看一次便對(duì)康熙的治國(guó)管理之道有了更深刻的認(rèn)識(shí)。他除了勤于政事,雄才大略,其馭人之術(shù)更是無(wú)...
    云才閱讀 570評(píng)論 0 0
  • 2017.2.6 正月初十 武漢 天氣晴 溫度4-12 今天開(kāi)始學(xué)習(xí)葉武濱的時(shí)間管理。整理筆記如下,在自己學(xué)習(xí)的同...
    飛鳴的海燕閱讀 378評(píng)論 0 0
  • Date:2017.12.10 Context: 昨天是星期六嘛按照慣例一起出去玩了啊真的很開(kāi)心的說(shuō) 嗚...
    書(shū)辰味的媛媛醬i閱讀 491評(píng)論 0 0

友情鏈接更多精彩內(nèi)容