Skip to content

Instantly share code, notes, and snippets.

@BrandonShega
Last active July 15, 2016 20:01
Show Gist options
  • Save BrandonShega/e66c9308f4709b36ef99132427cebbba to your computer and use it in GitHub Desktop.
Save BrandonShega/e66c9308f4709b36ef99132427cebbba to your computer and use it in GitHub Desktop.
Inverted View
//: Playground - noun: a place where people can play
import UIKit
import XCPlayground
func mask(viewToMask: UIView, maskRect: CGRect, invert: Bool = false) {
let maskLayer = CAShapeLayer()
let path = CGPathCreateMutable()
if (invert) {
CGPathAddRect(path, nil, viewToMask.bounds)
}
CGPathAddRoundedRect(path, nil, maskRect, 4.0, 4.0)
// CGPathAddRect(path, nil, maskRect)
maskLayer.path = path
if (invert) {
maskLayer.fillRule = kCAFillRuleEvenOdd
}
// Set the mask of the view.
viewToMask.layer.mask = maskLayer;
}
class MyViewController: UIViewController {
var overlay: UIView = {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 700))
view.backgroundColor = UIColor.blackColor()
view.alpha = 0.4
return view
}()
var window: UIView = {
let view = UIView(frame: CGRect(x: 50, y: 100, width: 275, height: 200))
view.backgroundColor = UIColor.clearColor()
return view
}()
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.whiteColor()
view.addSubview(overlay)
view.addSubview(window)
mask(overlay, maskRect: window.frame, invert: true)
}
}
XCPlaygroundPage.currentPage.liveView = MyViewController()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment