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這里面的類型

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容