博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS小技巧--UIView,UIButton,UIImageView等设置圆角,设置阴影,设置边框
阅读量:7238 次
发布时间:2019-06-29

本文共 1505 字,大约阅读时间需要 5 分钟。

hot3.png

UIView,UIButton,UIImageView等设置圆角,设置阴影,设置边框的方法

    在iOS开发中,任何可见视图都是继承于UIView的。    继承体系中,大部分UIView的属性适用于其任何孩子。

UIView,UIButton,UIImageView等设立圆角,设置阴影,设置边框的方法

UIView,UIButton,UIImageView等设立圆角,设置阴影,设置边框的方法

UIView,UIButton,UIImageView等设立圆角,设置阴影,设置边框的方法

UIView,UIButton,UIImageView等设立圆角,设置阴影,设置边框的方法

       而UIView的layer属性可以绘制UIView的各种效果。其实我们看到的View的动画实际上也是layer在绘制。

 

1、绘制圆角

 

    cornerView.layer.cornerRadius = 20;    cornerView.layer.masksToBounds = YES;

masksToBounds防止子元素溢出父视图。

如果一个正方形要设置成圆形,代码为:

 

    cornerView.layer.cornerRadius = cornerView.frame.size.height/2;    cornerView.layer.masksToBounds = YES;

 

 

2、绘制边框

 

    borderView.layer.borderWidth = 1.0;    borderView.layer.borderColor = [UIColor blackColor].CGColor;

注意此处使用的是CGColor而不是UIColor.

 

3、绘制阴影

 

    shadowView.layer.shadowColor = [UIColor redColor].CGColor;    shadowView.layer.shadowOffset = CGSizeMake(5.0, 5.0);    shadowView.layer.shadowOpacity = YES;

offset为偏移量,为正表示向frame x,y坐标增加的方向偏移。

opacity为透明度,默认为0,即表示透明的。所以我们要把opacity设置成1或者YES,表示不透明,也可以设置成0.5或者类似的值呈现半透明。

 

效果如下:

 

225732_uLL4_2331935.png

 

 

4、圆角阴影并存

 

  UIView *v=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];

    v.backgroundColor=[UIColor yellowColor];
    //v.layer.masksToBounds=YES;这行去掉
    v.layer.cornerRadius=10;
    v.layer.shadowColor=[UIColor redColor].CGColor;
    v.layer.shadowOffset=CGSizeMake(10, 10);
    v.layer.shadowOpacity=0.5;
    v.layer.shadowRadius=5;

    [self.view addSubview:v];

 

效果如下

 

 

/* When true an implicit mask matching the layer bounds is applied to

 * the layer (including the effects of the `cornerRadius' property). If
 * both `mask' and `masksToBounds' are non-nil the two masks are
 * multiplied to get the actual mask values. Defaults to NO.
 * Animatable. */

转载于:https://my.oschina.net/u/2331935/blog/403628

你可能感兴趣的文章
PL/SQL“ ORA-14551: 无法在查询中执行 DML 操作”解决
查看>>
Java基础学习总结(8)——super关键字
查看>>
a.b.c.d.e.f.g这样的字段变成d.e.f.g的几种方法
查看>>
MySQL5.7运行自定义函数报错
查看>>
C++中关联容器和序列式容器在erase迭代器时的区别
查看>>
细谈围城---我的启示录
查看>>
字符串shuffle
查看>>
Nginx+PHP配置
查看>>
如何修改Xenserver网卡的offload
查看>>
Jmeter安装手记
查看>>
FCKeditor配置步骤详解
查看>>
Ubuntu 12.04 Openstack Essex 安装(单节点)------01
查看>>
mysql 数据备份与还原
查看>>
CentOS 7网卡配置-nmcli
查看>>
Spring MVC
查看>>
[视频教学]Maclean教你用Vbox在Enterprise Linux 5上安装Oracle 10gR2 RAC
查看>>
【Oracle Database 12c新特性】Online Statistics Gathering for Bulk-Load 针对批量数据加载的在线统计信息收集...
查看>>
windows下nginx 配置 tomcat 集群
查看>>
iOS 保留到小数点后几位
查看>>
maven 常见故障处理
查看>>