创建一个圆底边的UIView

问题描述 投票:-3回答:1

我想要查看这个Bottom roundedenter image description here

swift view rounded-corners
1个回答
1
投票

我认为这非常接近你想要的:

rectView是您的初始视图;您可以使用+ 50的四边形曲线高度来调整曲线

let rectView = UIView(frame: CGRect(x: 50, y: 100, width: 200, height: 200))
    rectView.backgroundColor = .red
    view.addSubview(rectView)

    let arcBezierPath = UIBezierPath()
    arcBezierPath.move(to: CGPoint(x: 0, y: rectView.frame.height))
    arcBezierPath.addQuadCurve(to: CGPoint(x: rectView.frame.width, y: rectView.frame.height), controlPoint: CGPoint(x: rectView.frame.width / 2 , y: rectView.frame.height + 50 ))
    arcBezierPath.close()

    let shapeLayer = CAShapeLayer()

    shapeLayer.path = arcBezierPath.cgPath

    shapeLayer.fillColor = UIColor.red.cgColor

    rectView.layer.insertSublayer(shapeLayer, at: 0)
    rectView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
    rectView.layer.cornerRadius = 10
    rectView.layer.shadowRadius = 3
    rectView.layer.shadowColor = UIColor.black.cgColor
    rectView.layer.shadowOffset = CGSize(width: 0, height: 0)
    rectView.layer.shadowOpacity = 0.25

也许,甚至更好:您可以创建您的视图:

let rectView = UIView(frame: CGRect(x: 50, y: 100, width: 200, height: 200))
view.addSubview(rectView)

扩展UIView

extension UIView {
    func addBottomArc(ofHeight height: CGFloat, topCornerRadius: CGFloat) {
       let arcBezierPath = UIBezierPath()
       arcBezierPath.move(to: CGPoint(x: 0, y: frame.height))
       arcBezierPath.addQuadCurve(to: CGPoint(x: frame.width, y: frame.height), controlPoint: CGPoint(x: frame.width / 2 , y: frame.height + height ))
       arcBezierPath.close()

       let shapeLayer = CAShapeLayer()

       shapeLayer.path = arcBezierPath.cgPath
       shapeLayer.fillColor = UIColor.red.cgColor

       layer.insertSublayer(shapeLayer, at: 0)
       layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
       layer.cornerRadius = topCornerRadius
    }
}

所以你可以像这样设置高度和角半径:

rectView.addBottomArc(ofHeight: 50, topCornerRadius: 15)

而不是在视图中添加所需的阴影:

    rectView.layer.shadowRadius = 3
    rectView.layer.shadowColor = UIColor.black.cgColor
    rectView.layer.shadowOffset = CGSize(width: 0, height: 0)
    rectView.layer.shadowOpacity = 0.25
© www.soinside.com 2019 - 2024. All rights reserved.