创建一个带有圆形底边的 UIView [英] Create a UIView with rounded bottom edge

查看:58
本文介绍了创建一个带有圆形底边的 UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让底部圆形查看在此处输入图片描述

推荐答案

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

I think this is really close to what you want:

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

The rectView is your initial view; You can play with the + 50 of quad curve height to adjust the curve

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

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

Maybe, even better: You can create your view:

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

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

So you can set the height and the corner radius like this:

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

然后将您需要的阴影添加到您的视图中:

and than add the shadow you need to your view:

    rectView.layer.shadowRadius = 3
    rectView.layer.shadowColor = UIColor.black.cgColor
    rectView.layer.shadowOffset = CGSize(width: 0, height: 0)
    rectView.layer.shadowOpacity = 0.25

这篇关于创建一个带有圆形底边的 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆