1.使用

layer设置指定圆角 或者设定一个或几个圆角

码修 关注

2017.04.20 19:03* 字数 107 阅读 656评论 0喜欢 0

由于项目中需要给按钮左下 和左上加圆角,我司可爱的ui君并不想给我切图。
所以只有自己画了。由于很久没有用过这些知识,花了一些时间,
故记入笔记。
也可以用来画半圆。
选择要画圆角的位置只需要改变枚举即可。
有空能封装一下更好。

 
WX20170420-190022@2x.png

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.statusLabel.bounds

byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopLeft

cornerRadii:CGSizeMake(self.statusLabel.height/2, self.statusLabel.height/2)];

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

maskLayer.frame = self.statusLabel.bounds;

maskLayer.path = maskPath.CGPath;

self.statusLabel.layer.mask = maskLayer;

//////

UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake((bgView.width-125)/2, -10, 125, 10)];

whiteView.backgroundColor = [UIColor whiteColor];

if (@available(iOS 11.0, *)) {

whiteView.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;

    whiteview.layer.maskstoBound = true;

    whiteview.layer.coneradio = 5.0;

    whiteview.layer.shouldRasterize = YES; //圆角缓存

    whiteview.layer.rasterizationScale = [UIScreen mainScreen].scale;//提高流畅度

经测试 加上后两句之后流畅度有了提高。

} else {

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:whiteView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(5, 5)];

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

//设置大小

maskLayer.frame = whiteView.bounds;

//设置图形样子

maskLayer.path = maskPath.CGPath;

whiteView.layer.mask = maskLayer;

}

iOS设置圆角的方法及指定圆角的位置

  • 在iOS开发中,我们经常会遇到设置圆角的问题, 以下是几种设置圆角的方法:

第一种方法: 通过设置layer的属性

代码:


UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"willwang"]];
 
//只需要设置layer层的两个属性
//设置圆角
imageView.layer.cornerRadius =50
 
//将多余的部分切掉
imageView.layer.masksToBounds = YES;
imageView.layer.shouldRasterize = YES; //圆角缓存
imageView.layer.rasterizationScale = [UIScreen mainScreen].scale;//提高流畅度
经测试 加上后两句之后流畅度有了提高。
[self.view addSubview:imageView];
  • 这个实现方法里maskToBounds会触发离屏渲染(offscreen rendering),GPU在当前屏幕缓冲区外新开辟一个渲染缓冲区进行工作,也就是离屏渲染,这会给我们带来额外的性能损耗,如果这样的圆角操作达到一定数量,会触发缓冲区的频繁合并和上下文的的频繁切换,性能的代价会宏观地表现在用户体验上<掉帧>不建议使用.

第二种方法: 使用贝塞尔曲线UIBezierPath和Core Graphics框架画出一个圆角

效果图:

代码:


UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
 
 imageView.image = [UIImage imageNamed:@"willwang"];
 
    //开始对imageView进行画图
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 0.0);
 
    //使用贝塞尔曲线画出一个圆形图
    [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip];
 
    [imageView drawRect:imageView.bounds];
 
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
 
     //结束画图
    UIGraphicsEndImageContext();
 
    [self.view addSubview:imageView];
  • UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale) 各参数含义:
  • size —— 新创建的位图上下文的大小
  • opaque —— 透明开关,如果图形完全不用透明,设置为YES以优化位图的存储。
  • scale —— 缩放因子 iPhone 4是2.0,其他是1.0。虽然这里可以用[UIScreen mainScreen].scale来获取,但实际上设为0后,系统就会自动设置正确的比例

第三种方法: 使用Core Graphics框架画出一个圆角

代码:


    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 100, 100)];
 
    imageView.image = [UIImage imageNamed:@"willwang"];
 
    //开始对imageView进行画图
    UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
 
    // 获得图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
 
    // 设置一个范围
    CGRect rect = CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height);
 
    // 根据一个rect创建一个椭圆
    CGContextAddEllipseInRect(ctx, rect);
 
    // 裁剪
    CGContextClip(ctx);
 
    // 将原照片画到图形上下文
    [imageView.image drawInRect:rect];
 
    // 从上下文上获取剪裁后的照片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
 
    // 关闭上下文
    UIGraphicsEndImageContext();
 
    imageView.image = newImage;
 
    [self.view addSubview:imageView];

第四种方法: 使用CAShapeLayer和UIBezierPath设置圆角

代码:


UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
 
    imageView.image = [UIImage imageNamed:@"willwang"];
 
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:imageView.bounds.size];
 
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
 
    //设置大小
    maskLayer.frame = imageView.bounds;
 
    //设置图形样子
    maskLayer.path = maskPath.CGPath;
 
    imageView.layer.mask = maskLayer;
 
    [self.view addSubview:imageView];

第四种方法延伸 指定需要成为圆角的角

方法:


+ (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect
                          byRoundingCorners:(UIRectCorner)corners
                                cornerRadii:(CGSize)cornerRadii
corners参数指定了要成为圆角的角, 枚举类型如下:
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
    UIRectCornerTopLeft     = 1 <&lt; 0,
    UIRectCornerTopRight    = 1 <&lt; 1,
    UIRectCornerBottomLeft  = 1 <&lt; 2,
    UIRectCornerBottomRight = 1 <&lt; 3,
    UIRectCornerAllCorners  = ~0UL
};

效果图:

代码:


//设置视图位置和大小
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(120, 300, 100, 50)];
 
    //设置背景颜色
    myView.backgroundColor = [UIColor redColor];
 
    //添加
    [self.view addSubview:myView];
 
    //绘制圆角 要设置的圆角 使用“|”来组合
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:myView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(20, 20)];
 
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
 
    //设置大小
    maskLayer.frame = myView.bounds;
 
    //设置图形样子
    maskLayer.path = maskPath.CGPath;
 
    myView.layer.mask = maskLayer;
 
    UILabel *label = [[UILabel alloc]init];
 
    //添加文字
    label.text = @"willwang";
 
    //文字颜色
    label.textColor = [UIColor whiteColor];
 
    [myView addSubview: label];
 
    //自动布局
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
 
        make.center.equalTo(myView);
    }];

第五种方法 在storyboard或xib中设置

少而好学,如日出之阳; 壮而好学,如日中之光; 老而好学,如炳烛之明。

/////////////

第一种方法:通过设置layer的属性

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

//只需要设置layer层的两个属性

//设置圆角

imageView.layer.cornerRadius = imageView.frame.size.width / 2;

//将多余的部分切掉

imageView.layer.masksToBounds = YES;

[self.view addSubview:imageView];

注:禁用。。影响性能。

第二种方法:使用贝塞尔曲线UIBezierPath和Core Graphics框架画出一个圆角

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

imageView.image = [UIImage imageNamed:@"1"];

//开始对imageView进行画图

UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);

//使用贝塞尔曲线画出一个圆形图

[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip];

[imageView drawRect:imageView.bounds];

imageView.image = UIGraphicsGetImageFromCurrentImageContext();

//结束画图

UIGraphicsEndImageContext();

[self.view addSubview:imageView];

第三种方法:使用CAShapeLayer和UIBezierPath设置圆角

首先需要导入<AVFoundation/AVFoundation.h>

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

imageView.image = [UIImage imageNamed:@"1"];

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:imageView.bounds.size];

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

//设置大小

maskLayer.frame = imageView.bounds;

//设置图形样子

maskLayer.path = maskPath.CGPath;

imageView.layer.mask = maskLayer;

[self.view addSubview:imageView];

}
---------------------
作者:weixin_34381687
来源:CSDN
原文:https://blog.csdn.net/weixin_34381687/article/details/87515690
版权声明:本文为博主原创文章,转载请附上博文链接!

ios中为视图添加圆角的更多相关文章

  1. 浅谈iOS中的视图优化

    引言: 让我们来思考几个问题,你开发过的产品,它还有可以优化的地方吗?能增加它的帧率吗?能减少多余的CPU计算吗?是不是存在多余的GPU渲染?业务这点工作量对于越来越强大的设备面前显得微不足道,但作为 ...

  2. iOS中为网站添加图标到主屏幕

    1 <link rel="apple-touch-icon-precomposed" href="icon.png"/> 2 <link re ...

  3. Swift - iOS中各种视图控制器(View Controller)的介绍

    在iOS中,不同的视图控制器负责不同的功能,采用不同的风格向用户呈现信息.下面对各个视图控制器做个总结: 1,标准视图控制器 - View Controller 这个控制器只是用来呈现内容.通常会用来 ...

  4. [Xcode 实际操作]二、视图与手势-(5)给图像视图添加圆角效果

    目录:[Swift]Xcode实际操作 本文将演示给矩形图片添加圆角效果 import UIKit class ViewController: UIViewController { override ...

  5. IOS之表视图添加搜索栏

    下面是我们要实现的效果.本效果是在上一篇自定义表视图的基础上进行更改的.     1.将Search bar and search display拖动到ViewController中.不要添加Sear ...

  6. iOS中为网站添加图标到主屏幕以及增加启动画面

    虽然没有能力开发Native App,但还是可以利用iOS中Safari浏览器的特性小小的折腾一下,做一个伪Web App满足下小小的虚荣心的. 既然是在iOS中的Safari折腾的,那么代码中利用到 ...

  7. iOS中创建自定义的圆角按钮

    iOS中很多时候都需要用到指定风格的圆角按钮,尽管UIButton提供了一个方式创建圆角按钮: + (id)buttonWithType:(UIButtonType)buttonType;//指定bu ...

  8. iOS中通知的添加和移除

    我们都知道viewWillAppear:方法是在控制器的view将要显示的时候调用的,而viewWillDisappear:方法是在控制器的view将要隐藏的时候调用.很多时候我们根据自身需要将相关代 ...

  9. iOS 中 为UIView添加背景图片

    创建UIImage的方法有两种: UIImage *image = [UIImageimageNamed:@"image.jpg"];//这种不释放内存,要缓存 NSString ...

随机推荐

  1. hadoop3 配置second name node

    设置hdfs-site.xml <property> <name>dfs.http.address</name> <value>192.168.1.81 ...

  2. 【转】VMware Converter迁移linux系统虚拟机

    原始出处 今天接到一个需求,迁移一台linux的业务系统到vCenter云管理平台,其中遇到一些问题,于是进行了排错,这个过程与大家分享,下面把整个步骤进行截图说明. 1. 首先,登录到VMware  ...

  3. 每天进步一点点------DE2-70-TV例子说明

    module Reset_Delay(iCLK,iRST,oRST_0,oRST_1,oRST_2); input iCLK; input iRST; output reg oRST_0; outpu ...

  4. UVA 10795 A Different Task(模拟)

    题目链接:https://vjudge.net/problem/UVA-10795 一道比较有思维含量的一道题: 注意一种分治的思想和“除了柱子x和柱子y之外的那个柱子”编号的问题. 首先在初始局面和 ...

  5. JDK动态代理+反射实现动态修改注解属性值

    这是最近朋友的一个需求,正好闲来无聊有些时间,跟着研究一下,如有不正确的地方,欢迎大家指正~ 一.准备自定义注解 注:如何实现自定义注解,请移步百度. 二.实现 1.实现方式1:通过反射+动态代理动态 ...

  6. Markdown编辑器软件安装及问题处理

    一.Markdown简介 MarkdownPad是Windows下的一个多功能Markdown编辑器 Markdown是一门编辑语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式,可以用来对 ...

  7. Java中正负数的存储方式-正码 反码和补码

    Java中正负数的存储方式-正码 反码和补码 正码 我们以int 为例,一个int占用4个byte,32bits 0 存在内存上为 00000000 00000000 00000000 0000000 ...

  8. MSComm控件进行串口编程的基本步骤

    Visual C++为我们提供了一种好用的ActiveX控件Microsoft Communications Control(即MSComm)来支持应用程序对串口的访问,在应用程序中插入MSComm控 ...

  9. PHP固定长度字符串

    /** * 获取固定长度随机字符串 * @param $n * @return string * @throws Exception */ function gf_rand_str($n) { if ...

  10. The Preliminary Contest for ICPC Asia Xuzhou 2019 E XKC's basketball team(排序+二分)

    这题其实就是瞎搞,稍微想一想改一改就能过. 排序按值的大小排序,之后从后向前更新node节点的loc值,如果后一个节点的loc大于(不会等于)前一个节点的loc,就把前一个节点的loc值设置为后面的l ...