iOS开发_自定义设置圆角、指定位置设置圆角

【作者前言】:13年入圈,分享些本人工作中遇到的点点滴滴那些事儿,17年刚开始写博客,高手勿喷!以分享交流为主,欢迎各路豪杰点评改进!

1.应用场景:

处理UI控件时,为了好看,比如一些底层背景,需要指定位置设置不同的圆角。

2.实现目标:

利用相关API实现自定义设置圆角值,以左上、右上举例

3.代码说明:

        CGFloat radius = 10; // 圆角值大小
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20,100,200,200)];
        if (@available(iOS 11.0, *))
        {
            // 设置 左上 右上 圆角
            view.layer.cornerRadius = radius;
            view.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
        }else {
            // 设置 左上 右上 圆角
            UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(radius, radius)];
            CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
            maskLayer.frame = view.bounds;
            maskLayer.path = path.CGPath;
            view.layer.mask = maskLayer;
        }

注iOS11.0及以上有新的的API处理相应需求,CACornerMask 枚举值说明

typedef NS_OPTIONS (NSUInteger, CACornerMask)
{
  kCALayerMinXMinYCorner = 1U << 0,  /// 左上角
  kCALayerMaxXMinYCorner = 1U << 1,  /// 右上角
  kCALayerMinXMaxYCorner = 1U << 2,  /// 左下角
  kCALayerMaxXMaxYCorner = 1U << 3,  /// 右下角
};

iOS11.0 之前CACornerMask 枚举值说明

typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
    UIRectCornerTopLeft     = 1 << 0,  /// 左上角
    UIRectCornerTopRight    = 1 << 1,  /// 右上角
    UIRectCornerBottomLeft  = 1 << 2,  /// 左下角
    UIRectCornerBottomRight = 1 << 3,  /// 右下角
    UIRectCornerAllCorners  = ~0UL  /// 四个角
};

综上,提供一个简单的方法可设置任意位置的圆角值

/**
 @brief 设置任意角圆角
 @param view 需要设置的控件
 @param radius    圆角值
 @param positions  位置数组 - @1(左上), @2(右上), @3(左下), @4(右下)
 */
- (void)yp_setCornerRadiusWithView:(UIView *)view radius:(CGFloat)radius positions:(NSArray *)positions {
    // view.clipsToBounds = YES; 会触发离屏渲染问题 -  根据实际情况来决定是否放开
    if (@available(iOS 11.0, *)) {
        // 默认四个角
        CACornerMask corners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner | kCALayerMinXMaxYCorner | kCALayerMaxXMaxYCorner;
        if (positions.count == 1) { // 只设置了一个角
            NSInteger positionVal = [positions[0] integerValue];
            corners = positionVal == 1 ? kCALayerMinXMinYCorner : positionVal == 2 ? kCALayerMaxXMinYCorner : positionVal == 3 ? kCALayerMinXMaxYCorner : kCALayerMaxXMaxYCorner;
        }else if (positions.count == 2) { // 设置了两个角
            if ([positions containsObject:@1]) {
                if ([positions containsObject:@2]) {// 12
                    corners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
                }else if ([positions containsObject:@3]) {// 13
                    corners = kCALayerMinXMinYCorner | kCALayerMinXMaxYCorner;
                }else {// 14
                    corners = kCALayerMinXMinYCorner | kCALayerMaxXMaxYCorner;
                }
            }else if ([positions containsObject:@2]) {
                if ([positions containsObject:@3]) { // 23
                    corners = kCALayerMaxXMinYCorner | kCALayerMinXMaxYCorner;
                }else { // 24
                    corners = kCALayerMaxXMinYCorner | kCALayerMaxXMaxYCorner;
                }
            }else {// 34
                corners = kCALayerMinXMaxYCorner | kCALayerMaxXMaxYCorner;
            }
        }else if (positions.count == 3) { // 设置了三个角
            if ([positions containsObject:@1] && [positions containsObject:@2]) {
                if ([positions containsObject:@3]) { // 123
                    corners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner | kCALayerMinXMaxYCorner;
                }else { // 124
                    corners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner | kCALayerMaxXMaxYCorner;
                }
            }else if ([positions containsObject:@1] && [positions containsObject:@3]) { // 134
                corners = kCALayerMinXMinYCorner | kCALayerMinXMaxYCorner | kCALayerMaxXMaxYCorner;
            }else {// 234
                corners = kCALayerMaxXMinYCorner | kCALayerMinXMaxYCorner | kCALayerMaxXMaxYCorner;
            }
        }
        view.layer.cornerRadius = radius;
        view.layer.maskedCorners = corners;
        
    }else {
        // 默认四个角
        UIRectCorner corners = UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight;
        if (positions.count == 1) { // 只设置了一个角
            NSInteger positionVal = [positions[0] integerValue];
            corners = positionVal == 1 ? UIRectCornerTopLeft : positionVal == 2 ? UIRectCornerTopRight : positionVal == 3 ? UIRectCornerBottomLeft : UIRectCornerBottomRight;
        }else if (positions.count == 2) { // 设置了两个角
            if ([positions containsObject:@1]) {
                if ([positions containsObject:@2]) {// 12
                    corners = UIRectCornerTopLeft | UIRectCornerTopRight;
                }else if ([positions containsObject:@3]) {// 13
                    corners = UIRectCornerTopLeft | UIRectCornerBottomLeft;
                }else {// 14
                    corners = UIRectCornerTopLeft | UIRectCornerBottomRight;
                }
            }else if ([positions containsObject:@2]) {
                if ([positions containsObject:@3]) { // 23
                    corners = UIRectCornerTopRight | UIRectCornerBottomLeft;
                }else { // 24
                    corners = UIRectCornerTopRight | UIRectCornerBottomRight;
                }
            }else {// 34
                corners = UIRectCornerBottomLeft | UIRectCornerBottomRight;
            }
        }else if (positions.count == 3) { // 设置了三个角
            if ([positions containsObject:@1] && [positions containsObject:@2]) {
                if ([positions containsObject:@3]) { // 123
                    corners = UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft;
                }else { // 124
                    corners = UIRectCornerTopLeft | UIRectCornerTopRight  | UIRectCornerBottomRight;
                }
            }else if ([positions containsObject:@1] && [positions containsObject:@3]) { // 134
                corners = UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight;
            }else {// 234
                corners = UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight;            }
        }
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = view.bounds;
        maskLayer.path = path.CGPath;
        view.layer.mask = maskLayer;
    }
}

方法调用

    [self yp_setCornerRadiusWithView:self.imgV radius:16 positions:@[@1,@2]];

效果

image.png

你可能感兴趣的:(iOS开发_自定义设置圆角、指定位置设置圆角)