An open API service indexing awesome lists of open source software.

https://github.com/yzhong52/example-layermask

Example of Using Layer Mask
https://github.com/yzhong52/example-layermask

Last synced: 3 months ago
JSON representation

Example of Using Layer Mask

Awesome Lists containing this project

README

          

# Example of Using Layer Mask

First of all, we need to create a subclass from `UIView` (which you already did). But here is the code that I used:

private extension FloatingPoint {
var degreesToRadians: Self { return self * .pi / 180 }
var radiansToDegrees: Self { return self * 180 / .pi }
}

@IBDesignable class MaskView: UIView {
let startAngle: CGFloat = 180
let endAngle: CGFloat = 0

override func layoutSubviews() {
super.layoutSubviews()

// The multiplier determine how big the circle is
let multiplier: CGFloat = 3.0
let radius: CGFloat = frame.size.width * multipler
let maskLayer = CAShapeLayer()
let arcCenter = CGPoint(x: frame.size.width / 2, y: radius)
maskLayer.path = UIBezierPath(arcCenter: arcCenter,
radius: radius,
startAngle: startAngle.degreesToRadians,
endAngle: endAngle.degreesToRadians,
clockwise: true).cgPath
layer.mask = maskLayer
}
}

Then you can add a `MaskView` as a subView in the `ViewController`. Make sure select the view assigned the class `MaskView` in the storyboard:

Now we have a very simply view hierarchy:

Compiles the code and it's looking great:

If you want a scrollable subview that is masked, add it as a subview of the maskView. Here is how the view hierarchy looks after this:

And finally, this is how it looks running in the simulator:

[1]: https://i.stack.imgur.com/ibWDH.png
[2]: https://i.stack.imgur.com/9HSI2.png
[3]: https://i.stack.imgur.com/Gpj5l.png
[4]: https://i.stack.imgur.com/c4PA1.png
[5]: https://i.stack.imgur.com/r8YdN.png