这是一张图片,解释了我想做的所有事情:
我的问题是,我将如何构建 View 结构。表格 View 的标题应固定在表格顶部。但是,在表格 View 标题上方的最上面的图像呢?我是否必须在UIScrollView内添加表 View ?
可以通过CATransform3D
完成视差效果,但是我要如何实现自己想要的效果。有很多演示,但我想让它自定义。
最佳答案
您可以将 ImageView 添加到 View 中,例如-
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)
之后,在tableview委托(delegate)方法中,您可以添加
scrollviewDidScroll
方法,例如-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)
我希望这会有所帮助。如果我错了,请纠正我。