Skip to content

Instantly share code, notes, and snippets.

@brianmwadime
Forked from anitaa1990/CurvedUIView.swift
Created October 4, 2021 09:51
Show Gist options
  • Save brianmwadime/cdc46b0c893126fc82444c03fdd43b65 to your computer and use it in GitHub Desktop.
Save brianmwadime/cdc46b0c893126fc82444c03fdd43b65 to your computer and use it in GitHub Desktop.
A Swift extension of UIView to display a curved view
import UIKit
extension UIView {
/* Usage Example
* bgView.addBottomRoundedEdge(desiredCurve: 1.5)
*/
func addBottomRoundedEdge(desiredCurve: CGFloat?) {
let offset: CGFloat = self.frame.width / desiredCurve!
let bounds: CGRect = self.bounds
let rectBounds: CGRect = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.size.width, height: bounds.size.height / 2)
let rectPath: UIBezierPath = UIBezierPath(rect: rectBounds)
let ovalBounds: CGRect = CGRect(x: bounds.origin.x - offset / 2, y: bounds.origin.y, width: bounds.size.width + offset, height: bounds.size.height)
let ovalPath: UIBezierPath = UIBezierPath(ovalIn: ovalBounds)
rectPath.append(ovalPath)
// Create the shape layer and set its path
let maskLayer: CAShapeLayer = CAShapeLayer()
maskLayer.frame = bounds
maskLayer.path = rectPath.cgPath
// Set the newly created shape layer as the mask for the view's layer
self.layer.mask = maskLayer
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment