How to add image with parallax effect above UITableView header and keep header sticky?

后端 未结 3 1492
旧时难觅i
旧时难觅i 2020-12-24 10:27

Here is an image that explains everything I want to do:

\"enter

My question is

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-24 10:31

    You can add image view to the view like -

    let imageView = UIImageView()
    let lblName = UILabel()
    
    imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 300)
    imageView.image = UIImage.init(named: "poster")
    imageView.contentMode = .scaleAspectFill
    imageView.clipsToBounds = true
    view.addSubview(imageView)
    
    lblName.frame = CGRect(x: 20, y: 100, width: 200, height: 22)
    lblName.text = "Steve Jobs"
    lblName.textColor = UIColor.white
    lblName.font = UIFont.systemFont(ofSize: 26)
    lblName.clipsToBounds = true
    imageView.addSubview(lblName)
    

    After that in tableview delegate method you can add scrollviewDidScroll method like -

    let y = 300 - (scrollView.contentOffset.y + 300)
    let height = min(max(y, 60), 400)
    imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: height)
    lblName.frame = CGRect(x: 20, y: height - 30, width: 200, height: 22)
    

    I hope this will be helpful. Please correct me if I am wrong.

提交回复
热议问题