UIView 的圓角可以設(shè)置任意一邊或者兩邊三邊有圓角

今天因為工程里面的view設(shè)置的是左上邊沒有圓角长酗,所以特別寫了一個UIiView的分類方法,來實現(xiàn)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
  • 設(shè)置不同邊的圓角
  • @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

//調(diào)用

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子镇辉,更是在濱河造成了極大的恐慌,老刑警劉巖贴捡,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件忽肛,死亡現(xiàn)場離奇詭異,居然都是意外死亡烂斋,警方通過查閱死者的電腦和手機(jī)屹逛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來汛骂,“玉大人罕模,你說我怎么就攤上這事×辈t!?“怎么了淑掌?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長蝶念。 經(jīng)常有香客問我抛腕,道長,這世上最難降的妖魔是什么媒殉? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任担敌,我火速辦了婚禮,結(jié)果婚禮上廷蓉,老公的妹妹穿的比我還像新娘柄错。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布售貌。 她就那樣靜靜地躺著给猾,像睡著了一般。 火紅的嫁衣襯著肌膚如雪颂跨。 梳的紋絲不亂的頭發(fā)上敢伸,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天,我揣著相機(jī)與錄音恒削,去河邊找鬼池颈。 笑死,一個胖子當(dāng)著我的面吹牛钓丰,可吹牛的內(nèi)容都是我干的躯砰。 我是一名探鬼主播,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼携丁,長吁一口氣:“原來是場噩夢啊……” “哼琢歇!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起梦鉴,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤李茫,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后肥橙,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體魄宏,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年存筏,在試婚紗的時候發(fā)現(xiàn)自己被綠了宠互。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡椭坚,死狀恐怖名秀,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情藕溅,我是刑警寧澤匕得,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站巾表,受9級特大地震影響汁掠,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜集币,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一考阱、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧鞠苟,春花似錦乞榨、人聲如沸秽之。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽考榨。三九已至,卻和暖如春鹦倚,著一層夾襖步出監(jiān)牢的瞬間河质,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工震叙, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留掀鹅,地道東北人。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓媒楼,卻偏偏與公主長得像乐尊,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子划址,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內(nèi)容