Making custom shape imageview ios swift 4.2

Viewed 214
1 Answers

You can do this like:

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
}

And apply like this way:

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
Related