Fun with layers
Fun with layers
这篇文章的有些内容很奇怪,我根本就没有这种现象,所以暂时就这样吧
In this post, I’ll explain how to add a border, rounded corners, and drop shadow to any UIView
using some simple CALayer
properties. I’m not a CALayer
guru, but these few tricks from the layer world are particularly nice to know about.
These properties are present for every UIView
, since every view is actually drawn using an underlying CALayer
object owned by the UIView. You can do a lot without even knowing about CALayer
s because UIView
s encapsulate a lot of their functionality. These properties, however, are useful pieces that are not available directly through the UIView
interface.
To use these properties, you need to include the QuartzCore header:
Borders
To get a border, just set the borderColor
and borderWidth
properties of the layer, for example:
label.layer.borderWidth = 1;
The borderColor
is a CGColorRef
, which you can easily extract from any UIColor
as in the above example, which generates a border like this:
The border is just inside the frame of the view. Fractional values are allowed for the borderWidth
as well.
Corners
You can create rounded corners with code like this:
label.layer.borderColor = [UIColor grayColor].CGColor;
label.layer.borderWidth = 3;
just the cornerRadius
property is needed; I’ve also set the border to show how these properties work together:
As you can see in the example screenshot, the backgroundColor
of the UIView
is also restricted by the corner radius. You need to have clipsToBounds
set to YES
on your UIView
for rounded corners to work.
Shadows
You can also create a drop shadow that will be based on the alpha
component of whatever is drawn in your view. Often this will result in a
shadow just around the edges of the view. This example code on a UILabel
:
label.layer.shadowOpacity = 1.0;
label.layer.shadowRadius = 5.0;
label.layer.shadowOffset = CGSizeMake(0, 3);
label.clipsToBounds = NO;
results in this appearance:
In this case, you need clipsToBounds
to be NO
in order for a shadow outside the frame of your view to show up. Next
I’ll show you how you can actually combine rounded corners and drop
shadows, since I’m sure that’s what you really want to do now.
All together
Let’s say you want to present an image with a border, rounded
corners, and a drop shadow. The obvious problem from the above
explanations is that clipsToBounds
needs to be YES
for the rounded corners to work and NO
for the drop shadows. What’s a coder to do?
We can get around this apparent paradox by using the fact that the CALayer
treats its own background color (which may be image-based) differently than the UIView
‘s background color. Specifically, we can set clipsToBounds
to NO
and still achieve rounded corners by using direct properties of the layer instead of the UIView
. This code:
imgView.backgroundColor = [UIColor clearColor];
UIImage *image = [UIImage imageNamed:@"mandel.png"];
imgView.layer.backgroundColor = [UIColor colorWithPatternImage:image].CGColor;
// Rounded corners.
imgView.layer.cornerRadius = 10;
// A thin border.
imgView.layer.borderColor = [UIColor blackColor].CGColor;
imgView.layer.borderWidth = 0.3;
// Drop shadow.
imgView.layer.shadowColor = [UIColor blackColor].CGColor;
imgView.layer.shadowOpacity = 1.0;
imgView.layer.shadowRadius = 7.0;
imgView.layer.shadowOffset = CGSizeMake(0, 4);
Generates the image on the right, using the left image as the source (mandel.png):
Reference
I originally learned about this stuff from this blog post.
Post a comment or leave a trackback: Trackback URL.
Fun with layers的更多相关文章
- OpenLayers 3 中Layers的相关知识
昨天自己一遍又一遍的把API里Accessible map那个例子写下来,终于好像有熟悉一点点.找到一篇博文详细讲Layers的.整理一下贴出来(本来想在网上做笔记可以重新排版,比写在本子上要容易有结 ...
- Caffe学习系列(3):视觉层(Vision Layers)及参数
所有的层都具有的参数,如name, type, bottom, top和transform_param请参看我的前一篇文章:Caffe学习系列(2):数据层及参数 本文只讲解视觉层(Vision La ...
- Making raycast ignore multiple layers
I know how to make the raycast ignore a layer but I want it to ignore layers 9 and 10 but collide wi ...
- 【转】Unity3D中Layers和LayerMask解析
http://blog.csdn.net/yupu56/article/details/50441151 Unity中是用int32来表示32个Layer层.int32表示二进制一共有32位(0-31 ...
- 【Unity3D游戏开发】基础知识之Tags和Layers (三二)[转]
Tags和Layers分别表示是Unity引擎里面的标签和层,他们都是用来对GameObject进行标识的属性,Tags常用于单个GameObject,Layers常用于一组的GameObject.添 ...
- [Unity2D]Tags和Layers
Tags和Layers分别表示是Unity引擎里面的标签和层,他们都是用来对GameObject进行标识的属性,Tags常用于单个GameObject,Layers常用于一组的GameObject.添 ...
- Deep Learning: Assuming a deep neural network is properly regulated, can adding more layers actually make the performance degrade?
Deep Learning: Assuming a deep neural network is properly regulated, can adding more layers actually ...
- [原]Unity3D深入浅出 - 认识开发环境中的Layers面板
Layers(分层)下拉列表:用来控制Scene视图中游戏对象的显示,在下拉菜单中为勾选状态的物体将显示在Scene视图中. Everything:显示所有的游戏对象 Nothing:不显示任何游戏对 ...
- Investigation of Different Nets and Layers
Investigation of Different Nets and Layers Overview of AlexNet (MIT Places | Flickr Finetuned | Oxfo ...
随机推荐
- hdu 3400 Line belt 三分法
思路:要求最短时间从A到D,则走的路线一定是AB上的一段,CD上的一段,AB与CD之间的一段. 那么可以先三分得到AB上的一个点,在由这个点三分CD!! 代码如下: #include<iostr ...
- thinkphp 分组
分组 配置项: // 开启分组 'APP_GROUP_LIST'=>'Home,Admin', // 默认分组 'DEFAULT_GROUP'=>'Home', 涉及分组: 配置文件分组, ...
- redisb并发访问慢出现的问题
最近项目一上线,就问题颇多,本地测试,ok,上线后,大用户量的时候,顶不住.用了一个礼拜的时间发现的问题,总结下来. 项目是netty4.0,reids2.8,nginx等框架.目前是4台proxy服 ...
- How to change data dir of mysql?
# 1 copy orgin data dir of mysql to new one cp -R /var/lib/mysql /mysqldata chown mysql:mysql -R /my ...
- ubuntu终端颜色配置
对于刚接触ubuntu的同学们,打开终端(ctrl+alt+T),会发现里面都是一个颜色,不管是用户名.主机名还是命令都是白色,当然,用 ls 列出文件的时候是会多一种颜色的.即使这样,对开发人员来说 ...
- poj2750Potted Flower (线段树)
http://poj.org/problem?id=2750 之前做过类似的题 把一段的左连续最大.最小 右连续最大及最小及中间的连续更新出 就可以算出这段最大的连续和 注意不能全部加上 加上一特判 ...
- poj3378
统计长度为5的上升序列个数, 容易想到O(n^2)的dp f[k,i]:=Σf[k-1,j] (1<=j<i,a[i]>a[j]) ans:=Σf[5,i] 但是显然会超时,需要考虑 ...
- C#String与string大小写的区别
string是c#中的类 String是.net Framework的类 用string需要通过再次编译,所以直接用String速度会更快··· string是关键字,而String不是··· str ...
- c语言之extern关键字
1.定义 extern,外面的.外来的意思.那它有什么作用呢?举个例子:假设你在大街上看到一个黑皮肤绿眼睛红头发的美女(外星人?)或者帅哥.你的第一反应就是这人不是国产的. extern就相当于他们的 ...
- 【Struts】strust.xml中<result type="">所有类型详解
在默认时,<result>标签的type属性值是“dispatcher”(实际上就是转发,forward).开发人员可以根据自己的需要指定不同的类型,如redirect.stream等.如 ...