Skip to content

Instantly share code, notes, and snippets.

@BlueMilkApps
Last active December 29, 2015 20:02
Show Gist options
  • Save BlueMilkApps/b0f517738b9ffd76f697 to your computer and use it in GitHub Desktop.
Save BlueMilkApps/b0f517738b9ffd76f697 to your computer and use it in GitHub Desktop.
import UIKit
enum Direction { case In, Out }
protocol Dimmable { }
extension Dimmable where Self: UIViewController {
func dim(direction: Direction, color: UIColor = UIColor.blackColor(), alpha: CGFloat = 0.0, speed: Double = 0.0) {
switch direction {
case .In:
// Create and add a dim view
let dimView = UIView(frame: view.frame)
dimView.backgroundColor = color
dimView.alpha = 0.0
view.addSubview(dimView)
// Deal with Auto Layout
dimView.translatesAutoresizingMaskIntoConstraints = false
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[dimView]|", options: [], metrics: nil, views: ["dimView": dimView]))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[dimView]|", options: [], metrics: nil, views: ["dimView": dimView]))
// Animate alpha (the actual "dimming" effect)
UIView.animateWithDuration(speed) { () -> Void in
dimView.alpha = alpha
}
case .Out:
UIView.animateWithDuration(speed, animations: { () -> Void in
self.view.subviews.last?.alpha = alpha ?? 0
}, completion: { (complete) -> Void in
self.view.subviews.last?.removeFromSuperview()
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment