2016-06-10 29 views
0

以及什麼我想要實現這個庫https://github.com/Vinodh-G/ParallaxTableViewHeader但在實現這個庫時遇到的問題:添加模糊效果,而對視差頭(SWIFT)滾動

問題:

  1. 其對ObjC和我的母語是Swift,所以它很難理解發生了什麼事情

  2. 不知何故我實現了這個庫,但headerView的圖像不在中心,我試圖設置ContentMode到scaletofill,中心但它並沒有爲我工作

其出現這樣的(不是中心,直到我有一次滾動)

enter image description here 所以

滾動後:

enter image description here

所以我創造了我自己和它的工作的罰款只有一個PROBL EM是,我沒有任何模糊的效果還,所以如果有人知道它是如何工作或我如何才能增加我的HeaderView這個模糊的效果,那麼請讓我知道

我(SWIFT)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) 
} 

}

回答

1

好,而不是試圖模糊圖像,你可以在你的形象這當然是設置爲clearColor(中添加OverlayView的),然後當你向上滾動設置您的覆蓋以這種的背景色: -

UIColor(white: 1.0, alpha: scrollView.contentOffset.y/180) 

請注意,我曾經使用tableView設置兩種不同類型的單元格(一個parallaxHeaderViewCell包含imageView和overlayView,另一個是tableView的normalCell)。我已經寫了滾動是: -

func scrollViewDidScroll(scrollView: UIScrollView!) 

我也用我自己的代碼來實現視差,而不是實施第三方。它易於實施,沒有什麼大不了的!

+0

嗨@Sushree我剛剛試過你的答案,是的它的工作正常,但我仍然希望模糊讓我嘗試,如果我得到一個選項 –