美文网首页
iOS 之 UIBezierPath 切角

iOS 之 UIBezierPath 切角

作者: 小小的叶子随枫飘落 | 来源:发表于2017-01-16 10:07 被阅读140次

    在iOS中绘制圆角的方式挺常见的,但是要单独绘制右上、左上的一个或两个圆角的时候,一般的方法是实现不了的,可以使用UIBezierPath、CAShapeLayer来实现。


    UIBezierPath可以绘制任意形状的图形,而CAShapeLayer可以用来绘制所有能够通过CGPath来表示的形状,两者联合使用可以达到很好的切圆角的效果。

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor colorWithHex:0x66cc99];
        [button setTitle:@"退出当前账号" forState:UIControlStateNormal];
        button.frame = ccr(15, 50, SCREEN_WIDTH - 30, 44);
        
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:button.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(8, 8)];
        path.lineWidth = 1;
        UIColor *color = [UIColor redColor];
        [color set];
        
        CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
        shapeLayer.frame = button.bounds;
        shapeLayer.path = path.CGPath;
        button.layer.mask = shapeLayer;
       [self.view addSubview:button];
    

    效果如下:

    D59C5BCC-61FF-47C0-81C3-1286EE6D3A78.png

    当然 UIRectCorner 这个枚举有多中类型,可以根据自己的需要来进行设置。

    typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
        UIRectCornerTopLeft     = 1 << 0,
        UIRectCornerTopRight    = 1 << 1,
        UIRectCornerBottomLeft  = 1 << 2,
        UIRectCornerBottomRight = 1 << 3,
        UIRectCornerAllCorners  = ~0UL
    };
    

    相关文章

      网友评论

          本文标题:iOS 之 UIBezierPath 切角

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