iOS---view的任意一个或多个角变圆角

/**

 view 传入要变圆角的视图

 size自己根据需要设置角度大小

 后面的4个角 BOOL 1 是设置该角为圆角 0 不改变 

*/

- (void)renYiYuanJiao:(UIView *)view size:(CGSize)size left:(BOOL)left right:(BOOL)right bottomLeft:(BOOL)bottomLeft bottomRight:(BOOL)bottomRight {

    UIRectCorner Left = 0;

    UIRectCorner Right = 0;

    UIRectCorner BottomLeft = 0;

    UIRectCorner BottomRight = 0;

    if (left) {

       Left = UIRectCornerTopLeft;

    }

    if (right) {

        Right = UIRectCornerTopRight;;

    }

    if (bottomLeft) {

        BottomLeft = UIRectCornerBottomLeft;

    }

    if (bottomRight) {

        BottomRight = UIRectCornerBottomRight;

    }

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:Left|Right|BottomLeft|BottomRight cornerRadii:size];

    CAShapeLayer *maskLayer = [[CAShapeLayer allocinit];

    maskLayer.frame = view.bounds;

    maskLayer.path = maskPath.CGPath;

    view.layer.mask = maskLayer;

}

  1. 我们知道设置UIView的Layer的cornerRadius属性即可改变View的圆角。如果要单独设置某一个角或者2个,3个为圆角,就可以用下面方法。  

[objc]  view plain  copy
  1. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(120100100100)];  
  2. view.backgroundColor = [UIColor blueColor];  
  3. [self.view addSubview:view];  
  4.   
  5. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(1515)];  
  6. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];  
  7. maskLayer.frame = view.bounds;  
  8. maskLayer.path = maskPath.CGPath;  
  9. view.layer.mask = maskLayer;  

猜你喜欢

转载自blog.csdn.net/iotjin/article/details/80510809