美文网首页
iOS通过CALayer给视图画指定位置的圆角

iOS通过CALayer给视图画指定位置的圆角

作者: GoldenChan | 来源:发表于2019-07-30 01:16 被阅读0次

    通过创建UIView的分类来实现,这样处理的前提是视图的frame已经固定,如果是使用了约束,可以重写drawrect方法,并在此设置圆角

    .h文件如下

    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface UIView (CusCorner)
    
    /**
     设置指定位置的圆角
    
     @param rectCorner 圆角位置(该变量可使用或运算,用于同时设置多个圆角)
     @param radius 圆角半径
     @param borderWidth 边框宽度
     @param borderColor 边框颜色
     @param fillColor 填充色
     @param name 给子layer命名,防止重复设置增加开销
     */
    - (void)setupRoundedCorners:(UIRectCorner)rectCorner
                   cornerRadius:(CGFloat)radius
                    borderWidth:(CGFloat)borderWidth
                    borderColor:( UIColor * _Nullable )borderColor
                      fillColor:(UIColor * _Nullable)fillColor
                      layerName:(NSString * _Nullable)name;
    @end
    
    NS_ASSUME_NONNULL_END
    
    

    .m文件如下

    #import "UIView+CusCorner.h"
    
    @implementation UIView (CusCorner)
    - (void)setupRoundedCorners:(UIRectCorner)rectCorner
                   cornerRadius:(CGFloat)radius
                    borderWidth:(CGFloat)borderWidth
                    borderColor:( UIColor * _Nullable )borderColor
                      fillColor:(UIColor * _Nullable)fillColor
                      layerName:(NSString * _Nullable)name{
    //设置遮罩
        CAShapeLayer *mask=nil;
        if ([self.layer.mask.name isEqualToString:name]) {
            mask = self.layer.mask;
        } else {
            mask=[CAShapeLayer layer];
            mask.name = name;
        }
        UIBezierPath * path= [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:rectCorner cornerRadii:CGSizeMake(radius,radius)];
        mask.path=path.CGPath;
        mask.frame=self.bounds;
        self.layer.mask = mask;
    //设置边框及填充色
        __block CAShapeLayer *borderLayer = nil;
        if (name.length) {
            NSArray<CALayer *> *subLayers = self.layer.sublayers;
            [subLayers enumerateObjectsUsingBlock:^(CALayer * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if([obj.name isEqualToString:name]){
                    borderLayer = (CAShapeLayer *)obj;
                    *stop = YES;
                }
            }];
        }
        
        if (!borderLayer) {
            borderLayer=[CAShapeLayer layer];
            borderLayer.name = name;
            [self.layer addSublayer:borderLayer];
        }
        
        borderLayer.path=path.CGPath;
        if (fillColor) {
            borderLayer.fillColor = fillColor.CGColor;
        }
        if (borderColor) {
            borderLayer.strokeColor = borderColor.CGColor;
        }
        
        if (borderWidth > 0) {
            borderLayer.lineWidth = borderWidth;
        }
        borderLayer.frame = self.bounds;
    }
    @end
    

    调用如下

    //swift
    cusView.setupRoundedCorners([.topLeft , .topRight , .bottomLeft],
                                    cornerRadius: 12,
                                      borderWidth: 0, 
                                       borderColor: nil, 
                                                       fill:UIColor.clear, 
                                          layerName: "local")
    //oc
    [self.cusView setupRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight 
                                         cornerRadius:15 
                                           borderWidth:0 
                                            borderColor:nil 
                                                   fillColor:UIColor.clearColor 
                                              layerName:@"layername"];
    

    相关文章

      网友评论

          本文标题:iOS通过CALayer给视图画指定位置的圆角

          本文链接:https://www.haomeiwen.com/subject/zymwrctx.html