ドロップダウン画像のコントロールSwift
3175 ワード
//
// WPPathCover.swift
// ArtCircle
//
// Created by wupeng on 15/12/29.
//
//
import UIKit
class WPPathCover: UIView {
let OBSERVERKEYCONTENTOFFSET = "contentOffset"
private let blurView = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.Light))
private let bannerImageView:UIImageView = UIImageView(frame: CGRectZero)
var backgroundImage = "" {
didSet {
self.bannerImageView.kf_setImageWithURL(NSURL(string: self.backgroundImage)!, placeholderImage: UIImage(named: "DefaultLoading"))
}
}
var tableView:UITableView!
let parallaxHeight:CGFloat = tableHeaderViewHeight + 10
init(frame: CGRect,onTableView tableView:UITableView) {
super.init(frame: CGRectZero)
self.frame = CGRect(x: CGFloat.min, y: CGFloat.min, width: UIScreen.mainScreen().bounds.size.width, height: self.parallaxHeight)
self.bannerImageView.frame = self.bounds
self.bannerImageView.addSubview(self.blurView)
self.blurView.alpha = CGFloat.min
self.blurView.snp_makeConstraints { (make) -> Void in
make.edges.equalTo(self.bannerImageView).inset(UIEdgeInsets(top: CGFloat.min, left: CGFloat.min, bottom: CGFloat.min, right: CGFloat.min))
}
tableView.backgroundColor = UIColor.clearColor()
tableView.superview?.insertSubview(self.bannerImageView, belowSubview: tableView)
self.tableView = tableView
self.tableView.addObserver(self, forKeyPath: OBSERVERKEYCONTENTOFFSET, options: NSKeyValueObservingOptions.New, context: nil)
self.backgroundColor = UIColor.clearColor()
self.bannerImageView.contentMode = UIViewContentMode.ScaleAspectFill
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if object is UITableView {
if keyPath == OBSERVERKEYCONTENTOFFSET {
let tempTableView = object as! UITableView
self.offsetY = Float(tempTableView.contentOffset.y)
}
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var offsetY:Float = 0 {
didSet {
if self.offsetY < 0.0 {
var frame = self.bannerImageView.bounds
let alpha = CGFloat(fabsf(self.offsetY)) / self.parallaxHeight
print(alpha)
frame.size.height = self.parallaxHeight + CGFloat(fabsf(self.offsetY))
self.bannerImageView.frame = frame
if alpha > 0.1 {
self.blurView.alpha = alpha + 0.5
} else {
self.blurView.alpha = CGFloat.min
}
} else {
var frame = self.bannerImageView.bounds
frame.origin.y = CGFloat(-self.offsetY)
self.bannerImageView.frame = frame
}
}
}
deinit {
self.tableView.removeObserver(self, forKeyPath: OBSERVERKEYCONTENTOFFSET)
}
}