Last active
November 9, 2017 07:46
-
-
Save iAmrSalman/cba2980609ee6584b4e4e25637915e68 to your computer and use it in GitHub Desktop.
[UIViewExtensions] extensions for UIView to reduce dev time #uiview #extension #swift3
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 | |
public enum Shape { | |
case rectangle | |
case rounded(CGFloat) | |
case circular | |
} | |
extension UIView { | |
@IBInspectable var cornerRadius: CGFloat { | |
get { | |
return layer.cornerRadius | |
} | |
set { | |
layer.cornerRadius = newValue | |
layer.masksToBounds = newValue > 0 | |
} | |
} | |
@IBInspectable var borderWidth: CGFloat { | |
get { | |
return layer.borderWidth | |
} | |
set { | |
layer.borderWidth = newValue | |
} | |
} | |
@IBInspectable var borderColor: UIColor? { | |
get { | |
return self.borderColor | |
} | |
set { | |
layer.borderColor = newValue?.cgColor | |
} | |
} | |
@IBInspectable var shadowColor: UIColor? { | |
get { | |
return self.shadowColor | |
} | |
set { | |
layer.shadowColor = newValue?.cgColor | |
} | |
} | |
@IBInspectable var shadowRadius: CGFloat { | |
get { | |
return layer.shadowRadius | |
} | |
set { | |
layer.shadowRadius = newValue | |
} | |
} | |
@IBInspectable var shadowOpacity: Float { | |
get { | |
return layer.shadowOpacity | |
} | |
set { | |
layer.shadowOpacity = newValue | |
} | |
} | |
@IBInspectable var shadowOffset: CGSize { | |
get { | |
return layer.shadowOffset | |
} | |
set { | |
layer.shadowOffset = newValue | |
} | |
} | |
var shape: Shape? { | |
get { | |
return self.shape | |
} | |
set { | |
setShape(newValue!) | |
} | |
} | |
func setShape(_ shape: Shape) { | |
switch shape { | |
case .circular: | |
circulerContent() | |
case .rounded(let cornerRadius): | |
self.cornerRadius = cornerRadius | |
case .rectangle: | |
self.cornerRadius = 0 | |
} | |
} | |
public func circulerContent() { | |
self.cornerRadius = self.bounds.height / 2 | |
layer.masksToBounds = true | |
} | |
func setShadow(color: UIColor = .black, radius: CGFloat = 2, opacity: Float = 0.15, offset: CGSize = CGSize(width: 0, height: 1)) { | |
shadowColor = color | |
shadowRadius = radius | |
shadowOpacity = opacity | |
shadowOffset = offset | |
} | |
func shadowView(color: UIColor, radius: CGFloat, opacity: Float, offset: CGSize) -> UIView { | |
let shadowView = UIView() | |
shadowView.frame = self.frame | |
shadowView.shadowColor = color.cgColor | |
shadowView.shadowOffset = offset | |
shadowView.shadowRadius = radius | |
shadowView.shadowOpacity = opacity | |
shadowView.layer.shadowPath = UIBezierPath(rect: shadowView.bounds).cgPath | |
return shadowView | |
} | |
func shadowView(color: UIColor, radius: CGFloat, opacity: Float, offset: CGSize = CGSize(width: 0 , height: 0), shape: Shape) -> UIView { | |
let shadowView = UIView() | |
shadowView.frame = self.frame | |
shadowView.shadowColor = color.cgColor | |
shadowView.shadowOffset = offset | |
shadowView.shadowRadius = radius | |
shadowView.shadowOpacity = opacity | |
switch shape { | |
case .rectangle, .circular: | |
shadowView.layer.shadowPath = UIBezierPath(rect: shadowView.bounds).cgPath | |
case .rounded(let cornerRadius): | |
shadowView.layer.shadowPath = UIBezierPath(roundedRect: shadowView.bounds, cornerRadius: cornerRadius).cgPath | |
} | |
shadowView.layer.shadowPath = UIBezierPath(rect: shadowView.bounds).cgPath | |
self.shape = shape | |
return shadowView | |
} | |
func fadeIn(_ view: UIView, duration: TimeInterval = 1.0, delay: TimeInterval = 0.0, completion: @escaping ((Bool) -> Void) = {(finished: Bool) -> Void in}) { | |
UIView.transition(with: view, duration: duration, options: UIViewAnimationOptions.transitionCrossDissolve, animations: { | |
view.isHidden = false | |
}, completion: completion) | |
} | |
func fadeOut(_ view: UIView, duration: TimeInterval = 1.0, delay: TimeInterval = 0.0, completion: @escaping (Bool) -> Void = {(finished: Bool) -> Void in}) { | |
UIView.transition(with: view, duration: duration, options: UIViewAnimationOptions.transitionCrossDissolve, animations: { | |
view.isHidden = true | |
}, completion: completion) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment