我想在 Swift 中的 ScrollView 底部创建 UIView 圆形

I want to Create UIView Rounded Shape on Bottom inside ScrollView In Swift

不在上边缘。我想在 Bottom Edges 和 ScrollView 内部这样做。 如何做到这一点

创建如下扩展

extension UIView {

    func round(corners: UIRectCorner, cornerRadius: Double) {

        let size = CGSize(width: cornerRadius, height: cornerRadius)
        let bezierPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: size)
        let shapeLayer = CAShapeLayer()
        shapeLayer.frame = self.bounds
        shapeLayer.path = bezierPath.cgPath
        self.layer.mask = shapeLayer
    }
}

你可以这样使用它

self.myView.round(corners: [.bottomLeft, .bottomRight], cornerRadius: 35)

希望对你有用...:)

extension UIView { 
    func roundCorners(corners: UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        layer.mask = mask
    }
}

用作

yourView.roundCorners(corners : [.bottomLeft, .bottomRight], radius : 20 )

此外,请在 viewDidLayoutSubviews() 中更新您的布局,否则您可能会在 soem 设备中遇到裁剪问题。

view's layer 上配置 maskedCornerscornerRadius,即

customView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
customView.layer.cornerRadius = customView.bounds.width / 2.0

这应该接近你想要的。如果不太正确,请使用贝塞尔路径点:

@IBDesignable
class MyRoundBottomView: UIView {

    override func layoutSubviews() {
        super.layoutSubviews()

        let y = bounds.size.height - 80.0

        let p1 = CGPoint(x: 0.0, y: y)
        let p2 = CGPoint(x: bounds.size.width, y: y)

        let cp1 = CGPoint(x: p1.x, y: bounds.size.height)
        let cp2 = CGPoint(x: bounds.size.width, y: bounds.size.height)

        let myBez = UIBezierPath()

        myBez.move(to: CGPoint(x: 0.0, y: y))

        myBez.addCurve(to: p2, controlPoint1: cp1, controlPoint2: cp2)

        myBez.addLine(to: CGPoint(x: bounds.size.width, y: 0.0))
        myBez.addLine(to: CGPoint.zero)

        myBez.close()

        let l = CAShapeLayer()
        l.path = myBez.cgPath
        layer.mask = l

    }

}

它被标记为 @IBDesignable,因此您可以添加一个 UIView(在 Storyboard 中)并将其自定义 Class 分配给 MyRoundBottomView,您将在设计时看到它-时间: