好吧,我想要实现的是在这个库中https://github.com/Vinodh-G/ParallaxTableViewHeader但是在实现这个库时遇到了问题:
问题 :
它打开了
ObjC
,我的母语Swift
很难理解引擎盖下发生了什么不知何故,我实现了这个库,但 headerView 的图像不在中心,我试图将 ContentMode 设置为 scaletofill ,中心但它对我不起作用
它看起来像这样(直到我滚动一次才在中心)
滚动后:
所以我创建了自己的,它工作正常只有一个问题是我还没有任何模糊效果,所以如果有人知道这是如何工作的,或者我如何在我的 HeaderView 上添加这种模糊效果,请告诉我
我的(斯威夫特) HeaderView :
class HeaderView: UIView {
var heightLayoutConstraint = NSLayoutConstraint()
var bottomLayoutConstraint = NSLayoutConstraint()
var containerView = UIView()
var containerLayoutConstraint = NSLayoutConstraint()
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.whiteColor()
// The container view is needed to extend the visible area for the image view
// to include that below the navigation bar. If this container view isn't present
// the image view would be clipped at the navigation bar's bottom and the parallax
// effect would not work correctly
containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.backgroundColor = UIColor.redColor()
self.addSubview(containerView)
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[containerView]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["containerView" : containerView]))
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("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.init()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.backgroundColor = UIColor.whiteColor()
imageView.clipsToBounds = true
imageView.contentMode = .ScaleAspectFill
imageView.image = UIImage(named: "cover")
containerView.addSubview(imageView)
containerView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("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)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
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)
}
}