Last active
December 12, 2017 07:08
Star
You must be signed in to star a gist
UIImageView clip showing image
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
extension UIImageView { | |
func clip(roundedRect: CGRect?, cornerRadius: CGFloat = 0, force: Bool = false) { | |
if force { | |
let bezierPath = UIBezierPath(roundedRect: roundedRect ?? bounds, cornerRadius: cornerRadius) | |
let maskLayer = CAShapeLayer() | |
maskLayer.path = bezierPath.cgPath | |
layer.mask = maskLayer | |
} else if layer.mask == nil { | |
let bezierPath = UIBezierPath(roundedRect: roundedRect ?? bounds, cornerRadius: cornerRadius) | |
let maskLayer = CAShapeLayer() | |
maskLayer.path = bezierPath.cgPath | |
layer.mask = maskLayer | |
} | |
} | |
func clip(cornerRadius: CGFloat = 0, force: Bool = false) { | |
clip(roundedRect: nil, cornerRadius: cornerRadius, force: force) | |
} | |
func clip(cornerRadius: CGFloat!) { | |
clip(cornerRadius: cornerRadius, force: false) | |
} | |
func clip(force: Bool!) { | |
clip(cornerRadius: 0, force: force) | |
} | |
func clipsToCircle() { | |
let radius = bounds.width < bounds.height ? bounds.width : bounds.height | |
clip(cornerRadius: radius/2.0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment