Skip to content

Instantly share code, notes, and snippets.

@Akhu
Created May 12, 2017 12:53
Show Gist options
  • Save Akhu/057e2a19e56be75e23d244dfcdc37846 to your computer and use it in GitHub Desktop.
Save Akhu/057e2a19e56be75e23d244dfcdc37846 to your computer and use it in GitHub Desktop.
Simple way to invert a mask on a layer with swift / quartzcore / ios
func mask(withRect rect: CGRect, inverse: Bool = false) {
let path = UIBezierPath(rect: rect)
let maskLayer = CAShapeLayer()
if inverse {
path.appendPath(UIBezierPath(rect: self.bounds))
maskLayer.fillRule = kCAFillRuleEvenOdd
}
maskLayer.path = path.CGPath
self.layer.mask = maskLayer
}
@Chibija
Copy link

Chibija commented Sep 29, 2023

This shows only ON and OFF, i.e. fully opaque or complete passthrough.

How would you modify this for gradient/gradation? Make the mask alpha only partially obfuscate the underlying image.

I believe you just need to apply the gradient/opaque to the UIView you are masking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment