Skip to content

Instantly share code, notes, and snippets.

@wtshm
Created January 23, 2018 10:04
Show Gist options
  • Save wtshm/20b8a8443be98c22273773cab22e9f2f to your computer and use it in GitHub Desktop.
Save wtshm/20b8a8443be98c22273773cab22e9f2f to your computer and use it in GitHub Desktop.
import UIKit
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 UIColor(cgColor: layer.borderColor!)
}
set {
layer.borderColor = newValue?.cgColor
}
}
func fadeIn(duration: TimeInterval, delay: TimeInterval = 0, targetAlpha: CGFloat = 1, completed: (() -> ())? = nil) {
if self.alpha == 1 {
self.alpha = 0
}
self.isHidden = false
UIView.animate(withDuration: duration,
delay: delay,
options: .curveEaseOut,
animations: {
self.alpha = targetAlpha
}) { finished in
completed?()
}
}
func fadeOut(duration: TimeInterval, delay: TimeInterval = 0, targetAlpha: CGFloat = 0, completed: (() -> ())? = nil) {
UIView.animate(withDuration: duration,
delay: delay,
options: .curveEaseOut,
animations: {
self.alpha = targetAlpha
}) { [weak self] finished in
if targetAlpha == 0 {
self?.isHidden = true
}
completed?()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment