UIView的圆角曲线边

问题描述 投票:0回答:1

我试图通过这个扩展添加圆形边缘,但是我在UIView得到适当的圆形iPhone X但是当我尝试在iPhone 6s中运行相同的代码时,它就像没有弯曲,试图增加desiredCurve仍然无法看到曲线。

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
    }
}

enter image description here

swift xcode cashapelayer
1个回答
0
投票

你可以在橙色视图上使用Layer.corner.Radius

首先把视图宽度大于超级视图:

@IBOutlet weak var orangeView: UIView!

self.orangeView.frame. width = self.view.frame + 50
self.orangeView.layer.corner.Radius = self.orangeView.frame.height /2
© www.soinside.com 2019 - 2024. All rights reserved.