如何在UITableView标题上方添加具有视差效果的图像并保持标题粘滞? [英] How to add image with parallax effect above UITableView header and keep header sticky?

查看:180
本文介绍了如何在UITableView标题上方添加具有视差效果的图像并保持标题粘滞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一张图片,解释了我想要做的所有事情:

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

我的问题是,我将如何制作视图结构。表视图的标题应固定在表的顶部。但是在表视图标题上方的最顶层图像呢?我是否必须在UIScrollView中添加表格视图?

My question is, how would I make my view structure. The header of the table view should be fixed at top of the table. But what about the top most image that is above the table view header. Will I have to add the table view inside the UIScrollView ?

视差效果可以通过 CATransform3D 完成,但是如何我会实现我想要的,这就是我的问题。有很多演示,但我想让它完成自定义。

Parallax effect can be done by CATransform3D, but how would I achieve what I want, that is my question. There are lots of demos but I want to make it done custom.

推荐答案

您可以将图像视图添加到视图中,如 -

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)

之后在tableview委托方法中你可以添加 scrollviewDidScroll 方法如 -

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.

这篇关于如何在UITableView标题上方添加具有视差效果的图像并保持标题粘滞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆