View 圓角

    /*
     view的layer的實(shí)現(xiàn)
     
     通過(guò)view的layer直接設(shè)置的方式
     */
    UIImageView *redImgView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
    redImgView.contentMode = UIViewContentModeScaleToFill;
    redImgView.image = [UIImage imageNamed:@"11"];
    redImgView.layer.cornerRadius = redImgView.frame.size.width / 2;
    redImgView.layer.masksToBounds = YES;
    [self.view addSubview:redImgView];
    
    /*
     使用貝塞爾曲線UIBezierPath和Core Graphics實(shí)現(xiàn)
     
     BezierPath的實(shí)現(xiàn)方式繼承UIView,自己實(shí)現(xiàn)一個(gè)customview
     */
    UIImageView *blueImgView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 200, 100, 100)];
    blueImgView.contentMode = UIViewContentModeScaleAspectFit;
    blueImgView.image = [UIImage imageNamed:@"22"];
    //開(kāi)始對(duì)imageView進(jìn)行畫(huà)圖
    UIGraphicsBeginImageContextWithOptions(blueImgView.bounds.size, NO, 1.0);
    //使用貝塞爾曲線畫(huà)出一個(gè)圓形圖
    [[UIBezierPath bezierPathWithRoundedRect:blueImgView.bounds cornerRadius:blueImgView.frame.size.width] addClip];
    [blueImgView drawRect:blueImgView.bounds];
    blueImgView.image = UIGraphicsGetImageFromCurrentImageContext();
    //結(jié)束畫(huà)圖
    UIGraphicsEndImageContext();
    [self.view addSubview:blueImgView];
    
    /*
     使用CAShapeLayer和UIBezierPath實(shí)現(xiàn)
     
     通過(guò)bezizerpath設(shè)置一個(gè)路徑,加到目標(biāo)視圖的layer上。
     */
    UIImageView *grayImgView = [[UIImageView alloc]initWithFrame:CGRectMake(200, 50, 100, 100)];
    grayImgView.contentMode = UIViewContentModeScaleAspectFill;
    grayImgView.image = [UIImage imageNamed:@"33"];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:grayImgView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:grayImgView.bounds.size];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    //設(shè)置大小
    maskLayer.frame = grayImgView.bounds;
    //設(shè)置圖形樣子
    maskLayer.path = maskPath.CGPath;
    grayImgView.layer.mask = maskLayer;
    [self.view addSubview:grayImgView];
image.png
//設(shè)置view左側(cè)上UIRectCornerTopLeft 和 左側(cè)下UIRectCornerBottomLeft為5的圓角

UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft cornerRadii:CGSizeMake(5, 5)];

CAShapeLayer * layer = [[CAShapeLayer alloc]init];

layer.frame = view.bounds;

layer.path = path.CGPath;

view.layer.mask = layer;

//設(shè)置view 全部角為圓角

viewT.layer.cornerRadius = 10;//設(shè)置那個(gè)圓角大小

viewT.layer.masksToBounds = YES;//設(shè)置YES是保證添加的圖片覆蓋視圖的效果

iOS11 layer新增了maskedCorners屬性

typedef NS_OPTIONS (NSUInteger, CACornerMask)
{
  kCALayerMinXMinYCorner = 1U << 0,
  kCALayerMaxXMinYCorner = 1U << 1,
  kCALayerMinXMaxYCorner = 1U << 2,
  kCALayerMaxXMaxYCorner = 1U << 3,
};
//對(duì)左下角和右下角進(jìn)行圓角處理
if (@available(iOS 11.0, *)) {
        view2.layer.maskedCorners = kCALayerMaxXMaxYCorner | kCALayerMaxXMinYCorner;
    } else {
        // Fallback on earlier versions
    }

UIViewContentMode 視圖內(nèi)容的填充方式

@property(nonatomic) UIViewContentMode contentMode;
typedef NS_ENUM(NSInteger, UIViewContentMode) {
    UIViewContentModeScaleToFill,     //填充到整個(gè)視圖區(qū)域,不等比例拉伸。
    UIViewContentModeScaleAspectFit,  //長(zhǎng)寬等比填充視圖區(qū)域,當(dāng)某一個(gè)邊到達(dá)視圖邊界的時(shí)候就不再拉伸,保證內(nèi)容的長(zhǎng)寬比是不變的同時(shí)盡可能的填充視圖區(qū)域。
    UIViewContentModeScaleAspectFill, //長(zhǎng)寬等比填充視圖區(qū)域,當(dāng)某一個(gè)邊到達(dá)視圖邊界的時(shí)候還繼續(xù)拉伸,直到另一個(gè)方向達(dá)到視圖邊界。內(nèi)容的長(zhǎng)寬比不變的同時(shí)填滿整個(gè)視圖區(qū)域,不顯示超過(guò)的部分。
    UIViewContentModeRedraw,          //重繪視圖邊界
    UIViewContentModeCenter,          //視圖居中
    UIViewContentModeTop,             //視圖頂部對(duì)齊
    UIViewContentModeBottom,          //視圖底部對(duì)齊
    UIViewContentModeLeft,            //視圖左側(cè)對(duì)齊
    UIViewContentModeRight,           //視圖右側(cè)對(duì)齊
    UIViewContentModeTopLeft,         //視圖左上角對(duì)齊
    UIViewContentModeTopRight,        //視圖右上角對(duì)齊
    UIViewContentModeBottomLeft,      //視圖左下角對(duì)齊
    UIViewContentModeBottomRight,     //視圖右下角對(duì)齊
};
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 在iOS11之前,view展示圓角的處理過(guò)程為: 如此,便可以顯示一個(gè)圓角的view: iOS11對(duì)圓角功能進(jìn)行了...
    愛(ài)抽煙的芭比閱讀 9,309評(píng)論 6 28
  • 轉(zhuǎn)載:http://www.lxweimin.com/p/32fcadd12108 每個(gè)UIView有一個(gè)伙伴稱為l...
    F麥子閱讀 6,312評(píng)論 0 13
  • 每個(gè)UIView有一個(gè)伙伴稱為layer,一個(gè)CALayer。UIView實(shí)際上并沒(méi)有把自己畫(huà)到屏幕上;它繪制本身...
    shenzhenboy閱讀 3,155評(píng)論 0 17
  • 最近在進(jìn)行項(xiàng)目性能的優(yōu)化,遇到view圓角優(yōu)化的問(wèn)題,有一些粗略的看法,現(xiàn)總結(jié)一下。設(shè)置圓角目前知道的有四種方法:...
    無(wú)神閱讀 8,596評(píng)論 13 10
  • 想給視圖設(shè)置圓角,常用的做法是將IB中的控件拖線到控制器,然后設(shè)置其圓角半徑,以UIImageView為例,默認(rèn)的...
    CoderAO閱讀 30,375評(píng)論 6 33