forked from romansorochak/ParallaxHeader
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86f7182
commit 01e7a44
Showing
4 changed files
with
180 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// | ||
// UIView+Blur.swift | ||
// Blur | ||
// | ||
// Created by Roman Sorochak <[email protected]> on 6/27/17. | ||
// Copyright © 2017 MagicLab. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
extension UIView { | ||
|
||
private struct AssociatedKeys { | ||
static var descriptiveName = "AssociatedKeys.DescriptiveName.blurView" | ||
} | ||
|
||
private (set) var blurView: BlurView { | ||
get { | ||
if let blurView = objc_getAssociatedObject( | ||
self, | ||
&AssociatedKeys.descriptiveName | ||
) as? BlurView { | ||
return blurView | ||
} | ||
self.blurView = BlurView(to: self) | ||
return self.blurView | ||
} | ||
set(blurView) { | ||
objc_setAssociatedObject( | ||
self, | ||
&AssociatedKeys.descriptiveName, | ||
blurView, | ||
.OBJC_ASSOCIATION_RETAIN_NONATOMIC | ||
) | ||
} | ||
} | ||
|
||
class BlurView { | ||
|
||
private var superview: UIView | ||
private var blur: UIVisualEffectView? | ||
private var editing: Bool = false | ||
private (set) var blurContentView: UIView? | ||
private (set) var vibrancyContentView: UIView? | ||
|
||
var animationDuration: TimeInterval = 0.1 | ||
|
||
/** | ||
* Blur style. After it is changed all subviews on | ||
* blurContentView & vibrancyContentView will be deleted. | ||
*/ | ||
var style: UIBlurEffectStyle = .light { | ||
didSet { | ||
guard oldValue != style, | ||
!editing else { return } | ||
applyBlurEffect() | ||
} | ||
} | ||
/** | ||
* Alpha component of view. It can be changed freely. | ||
*/ | ||
var alpha: CGFloat = 0 { | ||
didSet { | ||
guard !editing else { return } | ||
if blur == nil { | ||
applyBlurEffect() | ||
} | ||
let alpha = self.alpha | ||
UIView.animate(withDuration: animationDuration) { | ||
self.blur?.alpha = alpha | ||
} | ||
} | ||
} | ||
|
||
init(to view: UIView) { | ||
self.superview = view | ||
} | ||
|
||
func setup(style: UIBlurEffectStyle, alpha: CGFloat) -> Self { | ||
self.editing = true | ||
|
||
self.style = style | ||
self.alpha = alpha | ||
|
||
self.editing = false | ||
|
||
return self | ||
} | ||
|
||
func enable(isHidden: Bool = false) { | ||
if blur == nil { | ||
applyBlurEffect() | ||
} | ||
|
||
self.blur?.isHidden = isHidden | ||
} | ||
|
||
private func applyBlurEffect() { | ||
blur?.removeFromSuperview() | ||
|
||
applyBlurEffect( | ||
style: style, | ||
blurAlpha: alpha | ||
) | ||
} | ||
|
||
private func applyBlurEffect(style: UIBlurEffectStyle, | ||
blurAlpha: CGFloat) { | ||
superview.backgroundColor = UIColor.clear | ||
|
||
let blurEffect = UIBlurEffect(style: style) | ||
let blurEffectView = UIVisualEffectView(effect: blurEffect) | ||
|
||
let vibrancyEffect = UIVibrancyEffect(blurEffect: blurEffect) | ||
let vibrancyView = UIVisualEffectView(effect: vibrancyEffect) | ||
blurEffectView.contentView.addSubview(vibrancyView) | ||
|
||
blurEffectView.alpha = blurAlpha | ||
|
||
superview.insertSubview(blurEffectView, at: 0) | ||
|
||
blurEffectView.addAlignedConstrains() | ||
vibrancyView.addAlignedConstrains() | ||
|
||
self.blur = blurEffectView | ||
self.blurContentView = blurEffectView.contentView | ||
self.vibrancyContentView = vibrancyView.contentView | ||
} | ||
} | ||
|
||
private func addAlignedConstrains() { | ||
translatesAutoresizingMaskIntoConstraints = false | ||
addAlignConstraintToSuperview(attribute: NSLayoutAttribute.top) | ||
addAlignConstraintToSuperview(attribute: NSLayoutAttribute.leading) | ||
addAlignConstraintToSuperview(attribute: NSLayoutAttribute.trailing) | ||
addAlignConstraintToSuperview(attribute: NSLayoutAttribute.bottom) | ||
} | ||
|
||
private func addAlignConstraintToSuperview(attribute: NSLayoutAttribute) { | ||
superview?.addConstraint( | ||
NSLayoutConstraint( | ||
item: self, | ||
attribute: attribute, | ||
relatedBy: NSLayoutRelation.equal, | ||
toItem: superview, | ||
attribute: attribute, | ||
multiplier: 1, | ||
constant: 0 | ||
) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters