制作自定义形状ImageView iOS Swift 4.2

时间:2019-01-04 10:19:30

标签: ios swift

我是ios新手。我试图使图像视图的底部边框(而不是整个底部的右侧)变成曲线。任何人都可以指导我该怎么做。

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

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以这样做:

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