ios - 如何在 UITableView 标题上方添加具有视差效果的图像并保持标题粘性?

标签 ios swift uiviewanimationtransition catransform3d uiview-hierarchy

这是一张图片,解释了我想做的一切:

enter image description here

我的问题是,我将如何构建我的 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)

希望对您有所帮助。如果我错了,请纠正我。

enter image description here

关于ios - 如何在 UITableView 标题上方添加具有视差效果的图像并保持标题粘性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30884996/

相关文章:

ios - 过渡时的 fatal error

android - 移动设备作为数据库服务器

ios - 路由 HTTP API 示例的问题

arrays - 创建一个函数来迭代和循环 Swift 中的数组

ios - 如何在swift中从被调用方法中识别调用方法

json - 如何通过 json(Dictionary) 生成模型类或将属性添加到没有属性的现有模型类

objective-c:执行多个 transitionWithView 动画 block

ios - 仅在 iOS 7 上向 UITableView 添加 subview 时出现 "Auto Layout still required"错误

objective-c - 如何将整数转换为 Objective-C 中相应的单词?

ios - UIView transitionWithView 不起作用