1

enter image description here

I made a view. Then I added a mask layer like this.

UIView *dd = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 400)];
dd.backgroundColor = [UIColor redColor];

dd.layer.masksToBounds = YES;
dd.layer.cornerRadius = 30.0f;
[self.view addSubview:dd];

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
CGRect maskRect = CGRectMake(100, 100, 200, 200);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, nil, maskRect);
[maskLayer setPath:path];
CGPathRelease(path);
dd.layer.mask = maskLayer;

and how can I cornerRadius on maskLayer?

I tried to

dd.layer.masksToBounds = YES;
dd.layer.cornerRadius = 30.0f;

it doesn't work. and other things too.

David Bang
  • 115
  • 10

2 Answers2

3

Any way may be you want like this :

enter image description here

Add QuartzCore Framework in your Project

// Import in .m file

  #import <QuartzCore/QuartzCore.h>

// Do this code

 UIView *dd = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
dd.backgroundColor = [UIColor redColor];

dd.layer.masksToBounds = YES;
dd.layer.borderColor = [UIColor whiteColor].CGColor;
[self.view addSubview:dd];

CGFloat cornerRadius = 0;
CGFloat borderWidth = 2;

UIColor *lineColor = [UIColor orangeColor];
CGRect maskRect = CGRectMake(100, 100, 200, 200);

//drawing
CGRect frame =maskRect;

CAShapeLayer *_shapeLayer = [CAShapeLayer layer];
//creating a path
CGMutablePathRef path = CGPathCreateMutable();

//drawing a border around a view
CGPathMoveToPoint(path, NULL, 0, frame.size.height - cornerRadius);
CGPathAddLineToPoint(path, NULL, 0, cornerRadius);
CGPathAddArc(path, NULL, cornerRadius, cornerRadius, cornerRadius, M_PI, -M_PI_2, NO);
CGPathAddLineToPoint(path, NULL, frame.size.width - cornerRadius, 0);
CGPathAddArc(path, NULL, frame.size.width - cornerRadius, cornerRadius, cornerRadius, -M_PI_2, 0, NO);
CGPathAddLineToPoint(path, NULL, frame.size.width, frame.size.height - cornerRadius);
CGPathAddArc(path, NULL, frame.size.width - cornerRadius, frame.size.height - cornerRadius, cornerRadius, 0, M_PI_2, NO);
CGPathAddLineToPoint(path, NULL, cornerRadius, frame.size.height);
CGPathAddArc(path, NULL, cornerRadius, frame.size.height - cornerRadius, cornerRadius, M_PI_2, M_PI, NO);

//path is set as the _shapeLayer object's path
_shapeLayer.path = path;
CGPathRelease(path);

_shapeLayer.backgroundColor = [[UIColor clearColor] CGColor];
_shapeLayer.frame = frame;
_shapeLayer.masksToBounds = NO;
_shapeLayer.fillColor = [[UIColor grayColor] CGColor];
_shapeLayer.strokeColor = [lineColor CGColor];
_shapeLayer.lineWidth = borderWidth;

//_shapeLayer is added as a sublayer of the view, the border is visible
[dd.layer addSublayer:_shapeLayer];
dd.layer.cornerRadius = cornerRadius;

May be it will work.

happy coding.

Dhaval Bhadania
  • 3,083
  • 1
  • 18
  • 34
1

You are making a layer from the path as I see. Therefore, rounder corners should be already in the path to take any effect. Try using not just rect but the Bezier curves. Create a UIBezierPath with cornered radius from your rect like this:

+ (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;

and use it for your layer's path

eagle.dan.1349
  • 570
  • 1
  • 8
  • 19