Skip to content

Instantly share code, notes, and snippets.

@MojtabaHs
Created August 1, 2020 22:14
Show Gist options
  • Save MojtabaHs/30e307aeada3b409fe20dcbb6cbceac6 to your computer and use it in GitHub Desktop.
Save MojtabaHs/30e307aeada3b409fe20dcbb6cbceac6 to your computer and use it in GitHub Desktop.
A class for masking any subclass of UIView with an Image.
@IBDesignable
class MaskableView: <#AnyUIViewSubclass#> {
var maskImageView = UIImageView()
@IBInspectable
var maskImage: UIImage? {
didSet {
maskImageView.image = maskImage
updateView()
}
}
override func layoutSubviews() {
super.layoutSubviews()
updateView()
}
func updateView() {
if maskImageView.image != nil {
maskImageView.frame = bounds
mask = maskImageView
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment