iOS给UIView切圆角

UIView切圆角

众所周知,给UIView切圆角有三种方法。
第一种是设置cornerRadius,但是这种切圆角的方式效率比较低,而且需要切四个圆角都要切,有时候不可以满足需求。
第二种使用贝塞尔曲线切圆角,画出一条路径,用mask来遮罩。代码简单,能够切任意一个位置的圆角
第三种就是纯手工,使用CGContextAddArcToPoint函数来画,显然这种效率比较高,但是易用性上肯定不如前两者。
最近研究了一下这三种方法,这里把代码贴出来供大家参考,水平有限,希望大家补充。

第一种 - cornerRadius

self.view.layer.cornerRadius = 5.0f;
    [self.view.layer setMasksToBounds:YES];

第二种 - 贝塞尔曲线

这里我是写一个UIView的分类,这样传入一个View,同时传入你要切哪几个圆角,这里也可以传入要切得值,童鞋们可以自己加一下。

//
//  UIView+CornerRadius.m
//  cornerRadius
//
//  Created by 李林 on 2016/11/26.
//  Copyright © 2016年 lee. All rights reserved.
//

#import "UIView+CornerRadius.h"

@implementation UIView (CornerRadius)

- (UIView *)clipCornerWithView:(UIView *)originView
                    andTopLeft:(BOOL)topLeft
                   andTopRight:(BOOL)topRight
                 andBottomLeft:(BOOL)bottomLeft
                andBottomRight:(BOOL)bottomRight
{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:originView.bounds
                                                   byRoundingCorners:(topLeft==YES ? UIRectCornerTopLeft : 0) |
                                                                     (topRight==YES ? UIRectCornerTopRight : 0) |
                                                                     (bottomLeft==YES ? UIRectCornerBottomLeft : 0) |
                                                                     (bottomRight==YES ? UIRectCornerBottomRight : 0)
                                                         cornerRadii:CGSizeMake(10, 10)];
    // 创建遮罩层
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = originView.bounds;
    maskLayer.path = maskPath.CGPath;   // 轨迹
    originView.layer.mask = maskLayer;

    return originView;
}

@end

第三种 - CGContextAddArcToPoint

这种方法参考的一个oschina的一个大神的博客,他讲的比较详细,我这里是借花献福罢了。

//
//  UIImageView+CornerRadius.m
//  cornerRadius
//
//  Created by 李林 on 2016/11/26.
//  Copyright © 2016年 lee. All rights reserved.
//

#import "UIImageView+CornerRadius.h"

@implementation UIImageView (CornerRadius)

- (UIImageView *)CreateImageViewWithFrame:(CGRect)rect
                       andBackground:(CGColorRef)color
                           andRadius:(CGFloat)radius{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    UIGraphicsBeginImageContext(imageView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();   // 设置上下文
    CGContextSetLineWidth(context, 1);                  // 边框大小
    CGContextSetStrokeColorWithColor(context, color);   // 边框颜色
    CGContextSetFillColorWithColor(context, color);     // 填充颜色

    CGFloat x = rect.origin.x;
    CGFloat y = rect.origin.y;
    CGFloat width = rect.size.width;
    CGFloat height = rect.size.height;
    CGContextMoveToPoint(context, x+width, y+radius/2);
    CGContextAddArcToPoint(context, x+width, y+height, x+width-radius/2, y+height, radius);
    CGContextAddArcToPoint(context, x, y+height, x, y+height-radius/2, radius);
    CGContextAddArcToPoint(context, x, y, x+radius/2, y, radius);
    CGContextAddArcToPoint(context, x+width, y, x+width, y+radius/2, radius);
    CGContextDrawPath(context, kCGPathFillStroke);
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();

    return imageView;
}

@end

你可能感兴趣的:(ios)