In my application - there are four buttons named as follows:
- Top - left
- Bottom - left
- Top - right
- Bottom - right
Above the buttons there is an image view (or a UIView).
Now, suppose a user taps on - top - left button. Above image / view should be rounded at that particular corner.
I am having some difficulty in applying rounded corners to the UIView.
Right now I am using the following code to apply the rounded corners to each view:
// imgVUserImg is a image view on IB.
imgVUserImg.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"any Url Here"];
CALayer *l = [imgVUserImg layer];
[l setMasksToBounds:YES];
[l setCornerRadius:5.0];
[l setBorderWidth:2.0];
[l setBorderColor:[[UIColor darkGrayColor] CGColor]];
Above code is applying the roundness to each of corners of supplied View. Instead I just wanted to apply roundness to selected corners like - top / top+left / bottom+right etc.
Is it possible? How?
I'd suggest defining a layer's mask. The mask itself should be a
CAShapeLayer
object with a dedicated path. You can use the next UIView extension (Swift 4.2):See this related question. You'll have to draw your own rectangle to a
CGPath
with some rounded corners, add theCGPath
to yourCGContext
and then clip to it usingCGContextClip
.You can also draw the rounded rect with alpha values to an image and then use that image to create a new layer which you set as your layer's
mask
property (see Apple's documentation).