美文网首页
username动画进入navigationBar

username动画进入navigationBar

作者: 桃花流水鳜鱼肥 | 来源:发表于2017-05-25 15:53 被阅读18次

ThirdCell

class ThirdCell: UITableViewCell {
    var usernameLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 16)
        label.textColor = UIColor.black
        return label
    }()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.contentView.addSubview(usernameLabel)
        self.usernameLabel.snp.makeConstraints { (make) in
            make.center.equalTo(self.contentView)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

ThirdController

class ThirdController: UIViewController {
    

    fileprivate var _tableView: UITableView!
    fileprivate var tableView:UITableView {
        get {
            if _tableView != nil {
                return _tableView
            }
            
            _tableView = UITableView()
            _tableView.backgroundColor = UIColor.clear
            
            _tableView.register(ThirdCell.self, forCellReuseIdentifier: "\(ThirdCell.self)")
            
            _tableView.delegate = self
            _tableView.dataSource = self
            
            return _tableView
        }
    }

    var titleView: UIView?
    var titleLabel: UILabel?
    let VIEW_WIDTH = UIScreen.main.bounds.width
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.automaticallyAdjustsScrollViewInsets = false
        
        self.view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view)
        }
        
        self.titleView = UIView(frame: CGRect(x: 0, y: 0, width: VIEW_WIDTH, height: 64))
        self.navigationItem.titleView = self.titleView!
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        if self.titleLabel == nil {
            let frame = self.titleView!.frame
            
            /*titleView的范围占navigationBar中部的一部分。下面通过创建一个coverView,这个coverView的范围可以覆盖
            navigationBar和状态栏,这样在处理titleLabel的变化时可以有个好参照
             */
            let coverView = UIView(frame: CGRect.init(x: frame.origin.x * -1, y: frame.origin.y * -1 - 20, width: VIEW_WIDTH, height: 64))
            coverView.clipsToBounds = true
            self.titleView?.addSubview(coverView)
            
            self.titleLabel = UILabel()
            self.titleLabel?.text = "来自天崩地裂的明天"
            self.titleLabel?.font = UIFont.systemFont(ofSize: 16)
            self.titleLabel?.textAlignment = .center
            self.titleLabel?.textColor = UIColor.black
            let height = self.titleLabel?.sizeThatFits(CGSize.init(width: VIEW_WIDTH, height: 100)).height
            self.titleLabel?.frame = CGRect(x: 0, y: 64, width: VIEW_WIDTH, height: height!)
            coverView.addSubview(self.titleLabel!)
            
        }
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offsetY = scrollView.contentOffset.y
        let labelHeight = self.titleLabel!.frame.height
        
        var y: CGFloat = 0
        if offsetY <= 56 - labelHeight/2 {
            y = 0
        }
        else if offsetY >= 76 {
            y = 22 + labelHeight/2
        }
        else {
            y = offsetY - 47
        }
        print(offsetY)
        self.titleLabel?.center = CGPoint(x: UIScreen.main.bounds.size.width/2, y: 64 - y + labelHeight/2)
    }
}
extension ThirdController:UITableViewDataSource,UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 240
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "\(ThirdCell.self)", for: indexPath) as! ThirdCell
        if indexPath.row == 0 {
            cell.usernameLabel.text = "来自天崩地裂的明天"
        }
        return cell
    }
}

相关文章

网友评论

      本文标题:username动画进入navigationBar

      本文链接:https://www.haomeiwen.com/subject/wolzxxtx.html