我想开发一个带有scrollView
的标题,类似于Revolut应用程序:
我考虑将一个scrollView
添加到另一个中,但我认为不会得到相同的结果。
这是我的代码:
import UIKit
import SnapKit
class ItemDetailViewController: UIViewController, UIScrollViewDelegate {
lazy var topBar = CryptoDetailTopBarView()
lazy var header = UIView()
lazy var chartView = UIView()
lazy var scrollView: UIScrollView = {
let view = UIScrollView()
view.contentSize = CGSize(width: 414, height: 2000)
view.backgroundColor = .systemGreen
view.bounces = true
return view
}()
lazy var name = UILabel()
lazy var symbol = UILabel()
lazy var type = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemGray
view.addSubview(topBar)
view.addSubview(header)
view.addSubview(scrollView)
header.addSubview(name)
header.addSubview(symbol)
header.addSubview(type)
scrollView.addSubview(chartView)
topBar.layer.zPosition = 20
topBar.backgroundColor = .blue
header.backgroundColor = .systemRed
name.textColor = .label
name.font = .boldSystemFont(ofSize: 34)
symbol.textColor = .label
type.textColor = .systemGray
chartView.backgroundColor = .white
chartView.isUserInteractionEnabled = true
self.topBar.title.text = "Title"
self.name.text = "Title"
self.symbol.text = "Text"
self.type.text = "Type"
scrollView.delegate = self
updateViewConstraints()
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if header.frame.minY < topBar.frame.maxY - name.frame.minY - name.frame.height/2 {
topBar.title.alpha = 1
} else {
topBar.title.alpha = 0
}
let y: CGFloat = scrollView.contentOffset.y
header.snp.remakeConstraints { make in
make.left.equalToSuperview()
make.top.equalTo(topBar.snp.bottom).inset(y)
make.width.equalToSuperview()
make.height.equalTo(150)
}
}
override func updateViewConstraints() {
topBar.snp.makeConstraints { make in
make.left.top.equalToSuperview()
make.width.equalToSuperview()
make.height.equalTo(90)
}
header.snp.makeConstraints { make in
make.left.equalToSuperview()
make.top.equalTo(topBar.snp.bottom)
make.width.equalToSuperview()
make.height.equalTo(150)
}
scrollView.snp.makeConstraints { make in
make.top.equalTo(header.snp.bottom)
make.centerX.bottom.width.equalToSuperview()
}
name.snp.makeConstraints { make in
make.top.equalToSuperview().offset(10)
make.left.equalToSuperview().offset(16)
make.width.equalTo(200)
make.height.equalTo(41)
}
symbol.snp.makeConstraints { make in
make.top.equalTo(name.snp.bottom).offset(10)
make.left.equalTo(name)
make.width.equalTo(50)
make.height.equalTo(20)
}
type.snp.makeConstraints { make in
make.top.equalTo(symbol)
make.left.equalTo(symbol.snp.right).offset(10)
make.width.equalTo(50)
make.height.equalTo(20)
}
chartView.snp.makeConstraints { make in
make.left.equalTo(scrollView).offset(16)
make.width.equalTo(scrollView).offset(-32)
make.top.equalTo(15)
make.height.equalTo(100)
}
super.updateViewConstraints()
}
fileprivate func getHeightOfHeaderView() -> CGFloat {
return header.frame.height
}
}
topBar
视图是一个小标题的view
,在我向下滚动时替代了大标题(例如Revolut应用)。我使用
SnapKit
根据scrollView
的偏移量更新约束。以下是我的效果:
![My app screen recording](https://istack.dev59.com/Vw2oT.gif)
chartView
的白色view
(scrollView
的一部分)移动速度比我的标题头快,这消除了在Revolut上推动的效果。此外,当我向下拖动
scrollView
时,我的白色view
被卡住了:应该有一个向下拉的效果。基本上我想做与Revolut相同的事情。App Store非特色应用程序具有相同的标题头。
我尝试过使用collectionView、tableView和scrollView(使用导航栏大标题),但它们无法实现我想要的效果。我像Revolut一样需要从头开始制作它。
顺便说一句,您可以轻松地执行我的代码,因为不需要故事板。