mike vorisis mike vorisis - 5 months ago 30
iOS Question

How to round only some corners of an image with Swift

I want to round only the two bottom corners of my image.
I found a lot of examples in Objective-C and almost nothing with Swift.

This is what I found but it gives me an error:

let rectShape = CAShapeLayer()

rectShape.bounds = self.image.frame
rectShape.position = self.image.center
rectShape.path = UIBezierPath(roundedRect: self.image.bounds, byRoundingCorners: .BottomLeft | .BottomRight, cornerRadii: CGSize(width: 20, height: 20)).CGPath

self.image.layer.backgroundColor = UIColor.greenColor().CGColor

self.image.layer.mask = rectShape


The error I get is in the line with
rectShape.path
and tells me that

No '|' candidates produce the expected contextual result type 'UIRectCorner'

Answer

In swift changes the way we put things like this. In objective c it was ok to write a | to separate options, but in swift you have to put it like an array:

let rectShape = CAShapeLayer()

    rectShape.bounds = self.image.frame
    rectShape.position = self.image.center
    rectShape.path = UIBezierPath(roundedRect: self.image.bounds,     byRoundingCorners: [.BottomLeft, .BottomRight], cornerRadii: CGSize(width: 20, height: 20)).CGPath

    self.image.layer.backgroundColor = UIColor.greenColor().CGColor

    self.image.layer.mask = rectShape