gpt4 book ai didi

ios - Swift - 如何向 tableView 添加具有视差效果的标题

转载 作者:行者123 更新时间:2023-12-01 18:41:05 25 4
gpt4 key购买 nike



在 tableView 上实现 header 的最有效方法是什么,它可以像 gif 一样为他的框架设置动画。
我需要一个可定制的方法,因为我想要一个可以成为 pageController 或类似的标题。

编辑:我尝试了在网上找到的几个库,它们是不同的。
我需要标题保持固定在顶部,如 gif 所示。
我尝试修改示例以完成此操作,但没有结果。

我试图改变 scrollViewdidscroll 但我失败了,因为偏移量没有线性变化,并且动画不起作用。
我无法为栏导航上的标题设置动画并确保它保持在其上方。

谢谢

最佳答案

干得好。您可以在这个简单的示例上构建您的实现:

import UIKit

class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()

let parallaxViewFrame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 200)
self.tableView.tableHeaderView = ParallaxHeaderView(frame: parallaxViewFrame)
}

override func scrollViewDidScroll(_ scrollView: UIScrollView) {
let headerView = self.tableView.tableHeaderView as! ParallaxHeaderView
headerView.scrollViewDidScroll(scrollView: scrollView)
}
}

final class ParallaxHeaderView: UIView {

fileprivate var heightLayoutConstraint = NSLayoutConstraint()
fileprivate var bottomLayoutConstraint = NSLayoutConstraint()
fileprivate var containerView = UIView()
fileprivate var containerLayoutConstraint = NSLayoutConstraint()

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = .white

containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.backgroundColor = UIColor.red

self.addSubview(containerView)
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[containerView]|",
options: NSLayoutFormatOptions(rawValue: 0),
metrics: nil,
views: ["containerView" : containerView]))

self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[containerView]|",
options: NSLayoutFormatOptions(rawValue: 0),
metrics: nil,
views: ["containerView" : containerView]))

containerLayoutConstraint = NSLayoutConstraint(item: containerView,
attribute: .height,
relatedBy: .equal,
toItem: self,
attribute: .height,
multiplier: 1.0,
constant: 0.0)
self.addConstraint(containerLayoutConstraint)

let imageView: UIImageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.backgroundColor = .white
imageView.clipsToBounds = true
imageView.contentMode = .scaleAspectFill
imageView.image = UIImage(named: "YourImage")

containerView.addSubview(imageView)
containerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[imageView]|",
options: NSLayoutFormatOptions(rawValue: 0),
metrics: nil,
views: ["imageView" : imageView]))

bottomLayoutConstraint = NSLayoutConstraint(item: imageView,
attribute: .bottom,
relatedBy: .equal,
toItem: containerView,
attribute: .bottom,
multiplier: 1.0,
constant: 0.0)

containerView.addConstraint(bottomLayoutConstraint)

heightLayoutConstraint = NSLayoutConstraint(item: imageView,
attribute: .height,
relatedBy: .equal,
toItem: containerView,
attribute: .height,
multiplier: 1.0,
constant: 0.0)

containerView.addConstraint(heightLayoutConstraint)
}

func scrollViewDidScroll(scrollView: UIScrollView) {
containerLayoutConstraint.constant = scrollView.contentInset.top;
let offsetY = -(scrollView.contentOffset.y + scrollView.contentInset.top);
containerView.clipsToBounds = offsetY <= 0
bottomLayoutConstraint.constant = offsetY >= 0 ? 0 : -offsetY / 2
heightLayoutConstraint.constant = max(offsetY + scrollView.contentInset.top, scrollView.contentInset.top)
}
}

关于ios - Swift - 如何向 tableView 添加具有视差效果的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42953360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com