Last active
May 18, 2022 16:22
-
-
Save anitaa1990/dd531515427cdecf80560a76f08cc870 to your computer and use it in GitHub Desktop.
A Swift extension of UIView to display a curved view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
for people trying to add a shadow to this, i was able to do it by using a second view