ioscalayeruibezierpathcgpathcashapelayer

Create a view with rounded corners (quarter circle)


I want to create a view as shown below

enter image description here

to achieve this I have tried the following:

UIView *myView = [[UIView alloc] init];
imgView.frame = CGRectMake(50, 150, 200, 200);
[self.view addSubview:imgView];

UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:myView.bounds
                                 byRoundingCorners:UIRectCornerTopRight
                                       cornerRadii:CGSizeMake(50.0, 50.0)];

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = myView.bounds;
maskLayer.path = maskPath.CGPath;
myView.layer.mask = maskLayer;

but it hasn't worked for me. Could someone help me out?


Solution

  • Pseudo code :

        UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(50, 150, 200, 200)];
    myView.backgroundColor = [UIColor blueColor];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:myView.bounds
                                     byRoundingCorners:UIRectCornerAllCorners
                                           cornerRadii:CGSizeMake(CGRectGetWidth(myView.frame), CGRectGetHeight(myView.frame))];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = myView.bounds;
    maskLayer.path = maskPath.CGPath;
    myView.layer.mask = maskLayer;
    
    
    CGRect frame = myView.frame;
    CGRect remainder, slice;
    
    CGRectDivide(frame, &slice, &remainder, CGRectGetHeight(frame)/2, CGRectMaxYEdge);
    
    NSLog(@"%@",NSStringFromCGRect(frame));
    NSLog(@"%@",NSStringFromCGRect(remainder));
    NSLog(@"%@",NSStringFromCGRect(slice));
    
    CGRect remainder2, slice2;
    CGRectDivide(remainder, &slice2, &remainder2, CGRectGetWidth(remainder)/2, CGRectMaxXEdge);
    
    NSLog(@"%@",NSStringFromCGRect(remainder));
    NSLog(@"%@",NSStringFromCGRect(remainder2));
    NSLog(@"%@",NSStringFromCGRect(slice2));
    
    myView.frame = remainder2;
    [self.view addSubview:myView];
    

    enter image description here

    hopefully y answer is clear .. 😁