Skip to content

Instantly share code, notes, and snippets.

@mpdroid
Created December 9, 2017 05:29
Show Gist options
  • Select an option

  • Save mpdroid/fd8dbeabf8dc874e78937db0273bf4bc to your computer and use it in GitHub Desktop.

Select an option

Save mpdroid/fd8dbeabf8dc874e78937db0273bf4bc to your computer and use it in GitHub Desktop.
Picture-frame using CAShapeLayer
import UIKit
import PlaygroundSupport
let bounds = CGRect(x: 0, y: 0, width: 400, height: 200)
let uiview = UIView(frame: bounds)
uiview.backgroundColor = UIColor.clear
PlaygroundPage.current.liveView = uiview
let shape = CGRect(x: 100, y: 50, width: 200, height: 100)
let maskLayer = CAShapeLayer()
maskLayer.frame = bounds
maskLayer.path = UIBezierPath(roundedRect: shape, cornerRadius: 16).cgPath
maskLayer.backgroundColor = UIColor.clear.cgColor
maskLayer.fillRule = kCAFillRuleEvenOdd
let frameview = UIView(frame: bounds)
frameview.backgroundColor = UIColor.red.withAlphaComponent(0.8)
frameview.layer.mask = maskLayer
let baseview = UIView(frame: bounds)
baseview.backgroundColor = UIColor.blue.withAlphaComponent(0.8)
uiview.addSubview(baseview)
uiview.insertSubview(frameview, aboveSubview: baseview)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment