代码之家  ›  专栏  ›  技术社区  ›  Abdulmoiz Ahmer

制作自定义形状图像视图iOS Swift 4.2

  •  2
  • Abdulmoiz Ahmer  · 技术社区  · 6 年前

    我刚接触过iOS。我正在尝试将图像视图的下边框(不是右下角的左下角)变为曲线。任何人都可以指导我怎么做。

    Here is the image of the border i want to acheive:

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  2
  •   Arie Pinto    6 年前

    您可以这样做:

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

    这样应用:

    let shapeLayer = CAShapeLayer(layer: imageView.layer)
    shapeLayer.path = self.curvedShapeFor(view: imageView,curvedPercent: 0.6).cgPath
    shapeLayer.frame = imageView.bounds
    shapeLayer.masksToBounds = true
    imageView.layer.mask = shapeLayer