View 圓角

    /*
     view的layer的實現(xiàn)
     
     通過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實現(xiàn)
     
     BezierPath的實現(xiàn)方式繼承UIView,自己實現(xiàn)一個customview
     */
    UIImageView *blueImgView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 200, 100, 100)];
    blueImgView.contentMode = UIViewContentModeScaleAspectFit;
    blueImgView.image = [UIImage imageNamed:@"22"];
    //開始對imageView進行畫圖
    UIGraphicsBeginImageContextWithOptions(blueImgView.bounds.size, NO, 1.0);
    //使用貝塞爾曲線畫出一個圓形圖
    [[UIBezierPath bezierPathWithRoundedRect:blueImgView.bounds cornerRadius:blueImgView.frame.size.width] addClip];
    [blueImgView drawRect:blueImgView.bounds];
    blueImgView.image = UIGraphicsGetImageFromCurrentImageContext();
    //結(jié)束畫圖
    UIGraphicsEndImageContext();
    [self.view addSubview:blueImgView];
    
    /*
     使用CAShapeLayer和UIBezierPath實現(xiàn)
     
     通過bezizerpath設(shè)置一個路徑,加到目標視圖的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è)置那個圓角大小

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,
};
//對左下角和右下角進行圓角處理
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,     //填充到整個視圖區(qū)域,不等比例拉伸。
    UIViewContentModeScaleAspectFit,  //長寬等比填充視圖區(qū)域,當某一個邊到達視圖邊界的時候就不再拉伸,保證內(nèi)容的長寬比是不變的同時盡可能的填充視圖區(qū)域。
    UIViewContentModeScaleAspectFill, //長寬等比填充視圖區(qū)域,當某一個邊到達視圖邊界的時候還繼續(xù)拉伸,直到另一個方向達到視圖邊界。內(nèi)容的長寬比不變的同時填滿整個視圖區(qū)域,不顯示超過的部分。
    UIViewContentModeRedraw,          //重繪視圖邊界
    UIViewContentModeCenter,          //視圖居中
    UIViewContentModeTop,             //視圖頂部對齊
    UIViewContentModeBottom,          //視圖底部對齊
    UIViewContentModeLeft,            //視圖左側(cè)對齊
    UIViewContentModeRight,           //視圖右側(cè)對齊
    UIViewContentModeTopLeft,         //視圖左上角對齊
    UIViewContentModeTopRight,        //視圖右上角對齊
    UIViewContentModeBottomLeft,      //視圖左下角對齊
    UIViewContentModeBottomRight,     //視圖右下角對齊
};
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容