这是一张图片,解释了我想做的一切:
我的问题是,我将如何构建我的 View 结构。表格 View 的标题应该固定在表格的顶部。但是在表格 View 标题上方的最上面的图像呢?我必须在 UIScrollView 中添加 TableView 吗?
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)
希望对您有所帮助。如果我错了,请纠正我。
关于ios - 如何在 UITableView 标题上方添加具有视差效果的图像并保持标题粘性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30884996/