Last active
December 29, 2015 20:02
-
-
Save BlueMilkApps/b0f517738b9ffd76f697 to your computer and use it in GitHub Desktop.
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
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