-
-
Save tarang9211/bcd761f7f736acd3600a33f17c8f0cd5 to your computer and use it in GitHub Desktop.
A Swift extension of UIView to display a curved view
This file contains 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