Swift 在UIView顶部创建曲线

Swift 在UIView顶部创建曲线,swift,uiview,uibezierpath,Swift,Uiview,Uibezierpath,我需要在UIView的顶部和底部创建一条曲线,我对底部使用了以下方法: func pathCurvedForView(givenView: UIView, curvedPercent:CGFloat) ->UIBezierPath { let arrowPath = UIBezierPath() arrowPath.move(to: CGPoint(x:0, y:0)) arrowPath.addLi

我需要在UIView的顶部和底部创建一条曲线,我对底部使用了以下方法:

 func pathCurvedForView(givenView: UIView, curvedPercent:CGFloat) ->UIBezierPath
        {
            let arrowPath = UIBezierPath()
            arrowPath.move(to: CGPoint(x:0, y:0))
            arrowPath.addLine(to: CGPoint(x:givenView.bounds.size.width, y:0))
            arrowPath.addLine(to: CGPoint(x:givenView.bounds.size.width, y:givenView.bounds.size.height))
            arrowPath.addQuadCurve(to: CGPoint(x:0, y:givenView.bounds.size.height), controlPoint: CGPoint(x:givenView.bounds.size.width/2, y:givenView.bounds.size.height-givenView.bounds.size.height*curvedPercent))
            arrowPath.addLine(to: CGPoint(x:0, y:0))
            arrowPath.close()
            
            return arrowPath
        }

    func applyCurvedPath(givenView: UIView,curvedPercent:CGFloat) {
        guard curvedPercent <= 1 && curvedPercent >= 0 else{
            return
        }
        
        let shapeLayer = CAShapeLayer(layer: givenView.layer)
        shapeLayer.path = self.pathCurvedForView(givenView: givenView,curvedPercent: curvedPercent).cgPath
        shapeLayer.frame = givenView.bounds
        shapeLayer.masksToBounds = true
        givenView.layer.mask = shapeLayer
    }
func-pathCurvedForView(给定视图:UIView,curvedPercent:CGFloat)->UIBezierPath
{
设arrowPath=UIBezierPath()
箭头路径。移动(到:CGPoint(x:0,y:0))
arrowPath.addLine(to:CGPoint(x:givenView.bounds.size.width,y:0))
arrowPath.addLine(to:CGPoint(x:givenView.bounds.size.width,y:givenView.bounds.size.height))
arrowPath.addQuadCurve(到:CGPoint(x:0,y:givenView.bounds.size.height),控制点:CGPoint(x:givenView.bounds.size.width/2,y:givenView.bounds.size.height givenView.bounds.size.height*curvedPercent))
arrowPath.addLine(到:CGPoint(x:0,y:0))
arrowPath.close()
返回箭头路径
}
func applyCurvedPath(给定视图:UIView,曲线百分比:CGFloat){
防护曲线百分比=0{
返回
}
let shapeLayer=CAShapeLayer(层:givenView.layer)
shapeLayer.path=self.pathCurvedForView(给定视图:给定视图,弯曲百分比:弯曲百分比)。cgPath
shapeLayer.frame=givenView.bounds
shapeLayer.masksToBounds=true
givenView.layer.mask=shapeLayer
}
它工作得很好,但只做了视图底部的工作,您知道如何在topView中实现它吗? 谢谢
让我们解释一下效果:

A             B
 + -------- +
 |          |
 |          |
 + -------- +
D            C
我们希望:

  • 从(0,0)开始:
    移动(到:)
  • 曲线高达B(宽度,0):
    addQuadCurve(到:控制点:)
  • 直接到C(宽度、高度):
    addLine(到:)
  • 曲线到D(0,高度):
    addQuadCurve(到:控制点:)
  • 直接到A(0,0):
    addLine(to:)
  • 接近
因此,替换

arrowPath.addLine(to: CGPoint(x:givenView.bounds.size.width, y:0))


让我们解释一下效果:

A             B
 + -------- +
 |          |
 |          |
 + -------- +
D            C
我们希望:

  • 从(0,0)开始:
    移动(到:)
  • 曲线高达B(宽度,0):
    addQuadCurve(到:控制点:)
  • 直接到C(宽度、高度):
    addLine(到:)
  • 曲线到D(0,高度):
    addQuadCurve(到:控制点:)
  • 直接到A(0,0):
    addLine(to:)
  • 接近
因此,替换

arrowPath.addLine(to: CGPoint(x:givenView.bounds.size.width, y:0))


arrowPath.addLine(to:CGPoint(x:givenView.bounds.size.width,y:0))替换为
arrowPath.addQuadCurve(to:CGPoint(x:givenView.bounds.size.width,y:0),控制点:CGPoint(x:givenView.bounds.size.width/2,y:givenView.bounds.size.height*curvedPercent))
替换
arrowPath.addLine(to:CGPoint(x:givenView.bounds.size.width,y:0))
with
arrowPath.addQuadCurve(to:CGPoint(x:givenView.bounds.size.width,y:0),控制点:CGPoint(x:givenView.bounds.size.width/2,y:givenView.bounds.size.height*curvedPercent))