【问题标题】:Create a UIView with rounded bottom edge创建一个带有圆形底边的 UIView
【发布时间】:2019-09-09 17:57:27
【问题描述】:

我要查看这个底部圆角enter image description here

【问题讨论】:

    标签: swift view rounded-corners


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-09
      • 2011-08-10
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      相关资源
      最近更新 更多