UITableViewCell部分圆角

UITableViewCell多分组下部分圆角

实现效果如图:

swift和oc版本都有

直接上demo::https://github.com/mrzhao12/UITableViewCell-

https://juejin.cn/post/7277362508813500457

.h文件:

#import

NS_ASSUME_NONNULL_BEGIN

@interface LLJSUIKitHelper : NSObject

-(void)LLJCView:(UIView*)viewcornerRadius:(NSArray*)cornerRadius;

@end

NS_ASSUME_NONNULL_END

.m文件

#import "LLJSUIKitHelper.h"

@implementation LLJSUIKitHelper

-(void)LLJCView:(UIView*)subViewcornerRadius:(NSArray*)cornerRadius{

    UIBezierPath *path = [[UIBezierPath alloc] init];

    NSMutableArray*cornerArray  = [NSMutableArray array];

    for(inti =0; i

        if(i ==0){

            [cornerArrayaddObject:@(UIRectCornerTopLeft)];

        }elseif(i ==1){

            [cornerArrayaddObject:@(UIRectCornerTopRight)];

        }elseif(i ==2){

            [cornerArrayaddObject:@(UIRectCornerBottomRight)];

        }elseif(i ==3){

            [cornerArrayaddObject:@(UIRectCornerBottomLeft)];

        }

    }

    path = [selfdrawRoundedRect:subView.boundsbyRoundingCorners:cornerArraycornerRadius:cornerRadius];

    CAShapeLayer *subLayer = [CAShapeLayer layer];

    subLayer.fillColor = [UIColor whiteColor].CGColor;

    subLayer.path= path.CGPath;

    subLayer.frame= subView.bounds;

    subView.layer.mask= subLayer;

}

-(UIBezierPath*)drawRoundedRect:(CGRect)rectbyRoundingCorners:(NSMutableArray*)byRoundingCornerscornerRadius:(NSArray*)cornerRadius{

    UIBezierPath *path = [[UIBezierPath alloc] init];

    CGPoint  startPoint =CGPointMake(rect.origin.x, rect.origin.y);

    //处理第一个角

    if ([byRoundingCorners containsObject:@(UIRectCornerTopLeft)]) {

        NSNumber*position = cornerRadius.firstObject;// ------>cornerRadius

        CGFloatmycg = position.floatValue;

        [pathmoveToPoint:CGPointMake(startPoint.x+ mycg, startPoint.y)];

    }else{

        [pathmoveToPoint:CGPointMake(startPoint.x, startPoint.y)];

    }

    //处理第二个角

    if ([byRoundingCorners containsObject:@(UIRectCornerTopRight)]) {

        NSNumber*position = [cornerRadiusobjectAtIndex:1];

        CGFloatmycg = position.floatValue;

        [pathaddLineToPoint:CGPointMake(startPoint.x+ rect.size.width- mycg, startPoint.y)];

        [pathaddArcWithCenter:CGPointMake(startPoint.x+ rect.size.width- mycg, startPoint.y+ mycg)radius:mycgstartAngle: (M_PI*3/2)endAngle:0clockwise:YES];

    }else{

        [pathaddLineToPoint:CGPointMake(startPoint.x+ rect.size.width, startPoint.y)];

    }

    //处理第三个角

    if ([byRoundingCorners containsObject:@(UIRectCornerBottomRight)]) {

        NSNumber*position = [cornerRadiusobjectAtIndex:2];

        CGFloatmycg = position.floatValue;

        [pathaddLineToPoint:CGPointMake(startPoint.x+ rect.size.width, startPoint.y+rect.size.height)];

        [pathaddArcWithCenter:CGPointMake(startPoint.x+rect.size.width-mycg, startPoint.y+rect.size.height-mycg)radius:mycgstartAngle:0endAngle: (M_PI/2)clockwise:YES];

    }else{

        [pathaddLineToPoint:CGPointMake(startPoint.x+ rect.size.width, startPoint.y+ rect.size.height)];

    }

//    //处理第四个角

    if ([byRoundingCorners containsObject:@(UIRectCornerBottomLeft)]) {

        NSNumber*position = [cornerRadiusobjectAtIndex:3];

        CGFloatmycg = position.floatValue;

        [pathaddLineToPoint:CGPointMake(startPoint.x+ mycg, startPoint.y+rect.size.height)];

        [pathaddArcWithCenter:CGPointMake(startPoint.x+mycg, startPoint.y+ rect.size.height-mycg)radius:mycgstartAngle: (M_PI/2)endAngle: (M_PI)clockwise:YES];

    }else{

        [pathaddLineToPoint:CGPointMake(startPoint.x, startPoint.y+rect.size.height)];

    }

//    //再次处理第一个角

    if ([byRoundingCorners containsObject:@(UIRectCornerTopLeft)]) {

        NSNumber*position = cornerRadius.firstObject;

         CGFloatmycg = position.floatValue;

        [pathaddLineToPoint:CGPointMake(startPoint.x, startPoint.y+ mycg)];

        [pathaddArcWithCenter:CGPointMake(startPoint.x+mycg, startPoint.y+ mycg)radius:mycgstartAngle: (M_PI)endAngle:(M_PI*3/2)clockwise:YES];

    }else{

        [pathaddLineToPoint:CGPointMake(startPoint.x, startPoint.y)];

    }

    returnpath;

}

@end

调用:直接在对应的TableView所在vc里写如下即可:

- (void)tableView:(UITableView*)tableViewwillDisplayCell:(UITableViewCell*)cellforRowAtIndexPath:(NSIndexPath*)indexPath{

    //组切圆角

    //方法一 判断第一个cell, 切出左上和又上圆角, 最后一个cell, 切出左下和右下圆角

    //方法二 使用带圆角的图片模拟

    LLJSUIKitHelper*kitHelper =  [[LLJSUIKitHelper alloc] init];

    if(indexPath.section==sectionSystem){

        [kitHelperLLJCView:cellcornerRadius:@[@(20),@(20),@(20),@(20)]];

  }  elseif(indexPath.section==sectionCommunity){

      [kitHelperLLJCView:cellcornerRadius:@[@(20),@(20),@(12),@(12)]];

  }

  elseif(indexPath.section==sectionWangyi){

      [kitHelperLLJCView:cellcornerRadius:@[@(20),@(10),@(40),@(20)]];

  }elseif(indexPath.section==sectionChat){


      if(self.recentSessions.count>0) {

          if(self.recentSessions.count==1) {

              [kitHelperLLJCView:cellcornerRadius:@[@(10),@(10),@(10),@(10)]];

          }else{

              if( indexPath.row==0) {

                  [kitHelperLLJCView:cellcornerRadius:@[@(10),@(10),@(0),@(0)]];

              }elseif(indexPath.row==self.recentSessions.count-1) {

//                  LLJSUIKitHelper.LLJCView(subView: cell, cornerRadius: [0,0,12,12])

                  [kitHelperLLJCView:cellcornerRadius:@[@(0),@(0),@(10),@(10)]];

              }

          }

      }


  }

  elseif(indexPath.section==sectionGray){

      [kitHelperLLJCView:cellcornerRadius:@[@(20),@(10),@(40),@(20)]];

  }

}

----------------

swift代码如下:

importUIKit

public class LLJSUIKitHelper {

    //UIView

    class func LLJView(backGroundColor: UIColor?, frame: CGRect?) -> UIView {


        letview =UIView()

        if(backGroundColor !=nil) {

            view.backgroundColor= backGroundColor

        }

        if(frame !=nil) {

            view.frame= frame!

        }

        returnview

    }


    //UIView 切统一圆角

    class func LLJCView(subView: UIView, cornerRadius: CGFloat) {

        // 圆角

        LLJCView(subView: subView,cornerRadius: [cornerRadius,cornerRadius,cornerRadius,cornerRadius])

    }


    //UIView 切指定不同圆角 cornerRadius = [8,10,12,14] 一次对应topLeft,topRight,bottomRight,bottomLeft的圆角半径

    class func LLJCView(subView: UIView, cornerRadius: [CGFloat]) {


        // 圆角

        varpath:UIBezierPath?

        varcornerArray = [UIRectCorner]()

        foriinstride(from:0,to: cornerRadius.count,by:1) {


            ifi ==0{

                cornerArray.append(UIRectCorner.topLeft)

            }elseifi ==1{

                cornerArray.append(UIRectCorner.topRight)

            }elseifi ==2{

                cornerArray.append(UIRectCorner.bottomRight)

            }elseifi ==3{

                cornerArray.append(UIRectCorner.bottomLeft)

            }

        }


        path =LLJBezierPath.drawRoundedRect(rect: subView.bounds,byRoundingCorners: cornerArray,cornerRadius: cornerRadius)

        letsubLayer =CAShapeLayer()

        subLayer.path= path!.cgPath

        subLayer.frame= subView.bounds

        subView.layer.mask= subLayer;

    }


    //按钮

    class func LLJButton(title: String?, titleColor: UIColor?, backGroundColor: UIColor?, titleFont: UIFont?, frame: CGRect?) -> UIButton {


        let button = UIButton(type: UIButton.ButtonType.custom)

        if(title !=nil) {

            button.setTitle(title,for:UIControl.State.normal)

        }

        if(titleColor !=nil) {

            button.setTitleColor(titleColor,for:UIControl.State.normal)

        }

        if(backGroundColor !=nil) {

            button.backgroundColor= backGroundColor

        }

        if(titleFont !=nil) {

            button.titleLabel?.font= titleFont

        }

        if(frame !=nil) {

            button.frame= frame!

        }

        returnbutton;

    }


    //Label

    class func LLJLabel(title: String?, titleColor: UIColor?, backGroundColor: UIColor?, titleFont: UIFont?, frame: CGRect?, numberOfLines: Int) -> UILabel {


        letlabel =UILabel()

        if(title !=nil) {

            label.text= title

        }

        if(titleColor !=nil) {

            label.textColor= titleColor

        }

        if(backGroundColor !=nil) {

            label.backgroundColor= backGroundColor

        }

        if(titleFont !=nil) {

            label.font= titleFont

        }

        if(frame !=nil) {

            label.frame= frame!

        }

        label.numberOfLines= numberOfLines

        returnlabel;

    }

}

importUIKit

class LLJBezierPath {

    /*

     * 画圆 椭圆

     * rect.w = rect.h 时画圆; rect.w != rect.h 画椭圆

     */

    class func drawCyle(rect: CGRect) -> UIBezierPath {


        letpath =UIBezierPath.init(ovalIn: rect)

        returnpath

    }


    /*

     * 画圆 圆弧

     * arcCenter: 圆心

     * radius: 半径

     * startAngle: 开始角度

     * endAngle: 结束角度

     * clockwise: 顺时针 yes 逆时针 no

     */

    class func drawCyle(arcCenter: CGPoint, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat, clockwise: Bool) -> UIBezierPath {


        letpath =UIBezierPath.init(arcCenter: arcCenter,radius: radius,startAngle: startAngle,endAngle: endAngle,clockwise: clockwise)

        returnpath

    }


    /*

     * 画矩形 统一圆角

     * rect: 矩形rect

     */

    class func drawRoundedRect(rect: CGRect, cornerRadius: CGFloat?) -> UIBezierPath {


        letpath =UIBezierPath.init(roundedRect: rect,cornerRadius: cornerRadius ??0.0)

        returnpath

    }


    /*

     * 画矩形 单一圆角

     * rect: 矩形rect

     */

    class func drawRoundedRect(rect: CGRect, byRoundingCorners: UIRectCorner, cornerRadius: CGFloat) -> UIBezierPath {


        letpath =UIBezierPath.init(roundedRect: rect,byRoundingCorners: byRoundingCorners,cornerRadii:CGSize(width: cornerRadius,height: cornerRadius))

        returnpath

    }


    /*

     * 画矩形 可变圆角

     * byRoundingCorners: 需要切的圆角

     * cornerRadius: 圆角半径

     * 注意:byRoundingCorners要和cornerRadius一一对应。

     * 如:[UIRectCorner.topLeft,UIRectCorner.topRight,UIRectCorner.bottomLeft,UIRectCorner.bottomRight] 对应 [10,5,5,10]

     */

    class func drawRoundedRect(rect: CGRect, byRoundingCorners: [UIRectCorner], cornerRadius: [CGFloat]) -> UIBezierPath {


        letpath =UIBezierPath()

        letstartPoint =CGPoint(x: rect.origin.x,y: rect.origin.y)


        //处理第一个角

        ifbyRoundingCorners.contains(UIRectCorner.topLeft) {


            letposition = byRoundingCorners.firstIndex(of:UIRectCorner.topLeft)

            path.move(to:CGPoint(x: startPoint.x+ cornerRadius[position!],y: startPoint.y))

        }else{

            path.move(to:CGPoint(x: startPoint.x,y: startPoint.y))

        }

        //处理第二个角

        ifbyRoundingCorners.contains(UIRectCorner.topRight) {


            letposition = byRoundingCorners.firstIndex(of:UIRectCorner.topRight)

            path.addLine(to:CGPoint(x: startPoint.x+ rect.width- cornerRadius[position!],y: startPoint.y))

            path.addArc(withCenter:CGPoint(x: startPoint.x+ rect.width- cornerRadius[position!],y: startPoint.y+ cornerRadius[position!]),radius: cornerRadius[position!],startAngle:CGFloat.pi*3/2,endAngle:0,clockwise:true)


        }else{

            path.addLine(to:CGPoint(x: startPoint.x+ rect.width,y: startPoint.y))

        }

        //处理第三个角

        ifbyRoundingCorners.contains(UIRectCorner.bottomRight) {


            letposition = byRoundingCorners.firstIndex(of:UIRectCorner.bottomRight)

            path.addLine(to:CGPoint(x: startPoint.x+ rect.width,y: startPoint.y+ rect.height- cornerRadius[position!]))

            path.addArc(withCenter:CGPoint(x: startPoint.x+ rect.width- cornerRadius[position!],y: startPoint.y+ rect.height- cornerRadius[position!]),radius: cornerRadius[position!],startAngle:0,endAngle:CGFloat.pi/2,clockwise:true)


        }else{

            path.addLine(to:CGPoint(x: startPoint.x+ rect.width,y: startPoint.y+ rect.height))

        }


        //处理第四个角

        ifbyRoundingCorners.contains(UIRectCorner.bottomLeft) {


            letposition = byRoundingCorners.firstIndex(of:UIRectCorner.bottomLeft)

            path.addLine(to:CGPoint(x: startPoint.x+ cornerRadius[position!],y: startPoint.y+ rect.height))

            path.addArc(withCenter:CGPoint(x: startPoint.x+ cornerRadius[position!],y: startPoint.y+ rect.height- cornerRadius[position!]),radius: cornerRadius[position!],startAngle:CGFloat.pi/2,endAngle:CGFloat.pi,clockwise:true)

        }else{

            path.addLine(to:CGPoint(x: startPoint.x,y: startPoint.y+ rect.height))

        }


        //再次处理第一个角

        ifbyRoundingCorners.contains(UIRectCorner.topLeft) {


            letposition = byRoundingCorners.firstIndex(of:UIRectCorner.topLeft)

            path.addLine(to:CGPoint(x: startPoint.x,y: startPoint.y+ cornerRadius[position!]))

            path.addArc(withCenter:CGPoint(x: startPoint.x+ cornerRadius[position!],y: startPoint.y+ cornerRadius[position!]),radius: cornerRadius[position!],startAngle:CGFloat.pi,endAngle:CGFloat.pi*3/2,clockwise:true)

        }else{

            path.addLine(to:CGPoint(x: startPoint.x,y: startPoint.y))

        }


        returnpath

    }



    /*

     * 画多边行 或折线

     * pointArray:多边形顶点数组,第一个点默认起点

     * closePath:true表示关闭,即多边形;false表示不关闭,即折线图

     */

    class func drawPolygon(pointArray: Array<CGPoint>, closePath: Bool) -> UIBezierPath {


        letpath =UIBezierPath()

        foriinstride(from:0,to: pointArray.count,by:1) {

            letpoint = pointArray[i]

            ifi ==0{

                path.move(to: point)

            }else{

                path.addLine(to: point)

            }

        }


        ifclosePath {

            path.close()

        }


        returnpath

    }


    /*

     * 画曲线 单曲线

     * startPoint: 起点

     * endPoint: 终点

     * controlPoint: 控制点

     */

    class func drawoQuadCurve(startPoint: CGPoint, endPoint: CGPoint, controlPoint: CGPoint) -> UIBezierPath {


        letpath =UIBezierPath()

        path.move(to: startPoint)

        path.addQuadCurve(to: endPoint,controlPoint: controlPoint)

        returnpath

    }


    /*

     * 画曲线 双曲线

     * startPoint: 起点

     * endPoint: 终点

     * controlPoint1: 控制点1

     * controlPoint2: 控制点2

     */

    class func drawCurve(startPoint: CGPoint, endPoint: CGPoint, controlPoint1: CGPoint, controlPoint2: CGPoint) -> UIBezierPath {


        letpath =UIBezierPath()

        path.move(to: startPoint)

        path.addCurve(to: endPoint,controlPoint1: controlPoint1,controlPoint2: controlPoint2)

        returnpath

    }

}

调用:

extension LLJWChatMainViewController: UITableViewDelegate {

    functableView(_tableView:UITableView,willDisplaycell:UITableViewCell,forRowAtindexPath:IndexPath) {

        //组切圆角

        //方法一 判断第一个cell, 切出左上和又上圆角, 最后一个cell, 切出左下和右下圆角

        //方法二 使用带圆角的图片模拟

        letsubArray =self.dataArray[indexPath.section]

        //切圆角

        ifsubArray.count==1{

            LLJSUIKitHelper.LLJCView(subView: cell,cornerRadius: [12,12,12,12])

        }else{

            ifindexPath.row==0{

                LLJSUIKitHelper.LLJCView(subView: cell,cornerRadius: [12,12,0,0])

            }elseif(indexPath.row== subArray.count-1) {

                LLJSUIKitHelper.LLJCView(subView: cell,cornerRadius: [0,0,12,12])

            }

        }

    }

}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,158评论 4 370
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,600评论 1 307
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,785评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,655评论 0 220
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,075评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,002评论 1 225
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,146评论 2 318
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,918评论 0 211
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,671评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,838评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,318评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,636评论 3 263
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,343评论 3 244
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,187评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,982评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,126评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,934评论 2 279

推荐阅读更多精彩内容