UIView 的圆角可以设置任意一边或者两边三边有圆角

今天因为工程里面的view设置的是左上边没有圆角,所以特别写了一个UIiView的分类方法,来实现UIview的某一边带有圆角

方法如下:
.h里

import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger,UILayoutCornerRadiusType) {
UILayoutCornerRadiusTop = 0,
UILayoutCornerRadiusLeft = 1,
UILayoutCornerRadiusBottom = 2,
UILayoutCornerRadiusRight = 3,
UILayoutCornerRadiusAll = 4,
UILayoutCornerdeleteTopleft = 5,
};
@interface UIView (CornerRadius)
/**

  • @author
  • 设置不同边的圆角
  • @param sideType 圆角类型
  • @param cornerRadius 圆角半径
    */
    -(void)UILayoutCornerRadiusType:(UILayoutCornerRadiusType)radiusType withCornerRadius:(CGFloat)cornerRadius;
    @end

.m里

import "UIView+CornerRadius.h"

@implementation UIView (CornerRadius)

  • (void)UILayoutCornerRadiusType:(UILayoutCornerRadiusType)sideType withCornerRadius:(CGFloat)cornerRadius
    {

    CGSize cornerSize = CGSizeMake(cornerRadius, cornerRadius);
    UIBezierPath *maskPath;

    switch (sideType) {
    case UILayoutCornerRadiusTop:
    {
    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
    byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)
    cornerRadii:cornerSize];
    }
    break;
    case UILayoutCornerRadiusLeft:
    {
    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
    byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerBottomLeft)
    cornerRadii:cornerSize];
    }
    break;
    case UILayoutCornerRadiusBottom:
    {
    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
    byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight)
    cornerRadii:cornerSize];
    }
    break;
    case UILayoutCornerRadiusRight:
    {
    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
    byRoundingCorners:(UIRectCornerTopRight|UIRectCornerBottomRight)
    cornerRadii:cornerSize];
    }
    break;
    case UILayoutCornerdeleteTopleft:
    {
    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
    byRoundingCorners:(UIRectCornerTopRight|UIRectCornerBottomRight|UIRectCornerBottomLeft)
    cornerRadii:cornerSize];
    }
    break;
    default:
    {
    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
    byRoundingCorners:UIRectCornerAllCorners
    cornerRadii:cornerSize];
    }
    break;
    }

    // Create the shape layer and set its path
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;

    // Set the newly created shape layer as the mask for the image view's layer
    self.layer.mask = maskLayer;

    [self.layer setMasksToBounds:YES];
    }
    @end

//调用

  • (void)viewDidLoad {
    [super viewDidLoad];

    UIView *whiteView =[[UIView alloc]init];
    whiteView.backgroundColor = [UIColor whiteColor];
    whiteView.frame = CGRectMake(30, 300, 200, 200);
    [whiteView UILayoutCornerRadiusType:5 withCornerRadius:3];
    [self.view addSubview:whiteView];
    }
    此处声明一下:可以修改代码,让其中三边有圆角,只需要添加类型然再在Case里添加 其实主要是添加 UIRectCornerTopRight|UIRectCornerBottomRight|UIRectCornerBottomLeft这里面的类型

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容