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 CALayers because UIViews 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:

#import <QuartzCore/QuartzCore.h>

Borders

To get a border, just set the borderColor and borderWidth properties of the layer, for example:

label.layer.borderColor = [UIColor blueColor].CGColor;
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.cornerRadius = 20;
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.shadowColor = [UIColor blackColor].CGColor;
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:

UIView *imgView = [[[UIView alloc] initWithFrame:imgFrame] autorelease];
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.

CALayer class reference

Tweet

This entry was written by Tyler, posted on May 8, 2010 at 3:14 pm, filed under UI code tips and tagged border, drop shadow, rounded corners, UIView. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
Post a comment or leave a trackback: Trackback URL.

Fun with layers的更多相关文章

  1. OpenLayers 3 中Layers的相关知识

    昨天自己一遍又一遍的把API里Accessible map那个例子写下来,终于好像有熟悉一点点.找到一篇博文详细讲Layers的.整理一下贴出来(本来想在网上做笔记可以重新排版,比写在本子上要容易有结 ...

  2. Caffe学习系列(3):视觉层(Vision Layers)及参数

    所有的层都具有的参数,如name, type, bottom, top和transform_param请参看我的前一篇文章:Caffe学习系列(2):数据层及参数 本文只讲解视觉层(Vision La ...

  3. 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 ...

  4. 【转】Unity3D中Layers和LayerMask解析

    http://blog.csdn.net/yupu56/article/details/50441151 Unity中是用int32来表示32个Layer层.int32表示二进制一共有32位(0-31 ...

  5. 【Unity3D游戏开发】基础知识之Tags和Layers (三二)[转]

    Tags和Layers分别表示是Unity引擎里面的标签和层,他们都是用来对GameObject进行标识的属性,Tags常用于单个GameObject,Layers常用于一组的GameObject.添 ...

  6. [Unity2D]Tags和Layers

    Tags和Layers分别表示是Unity引擎里面的标签和层,他们都是用来对GameObject进行标识的属性,Tags常用于单个GameObject,Layers常用于一组的GameObject.添 ...

  7. 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 ...

  8. [原]Unity3D深入浅出 - 认识开发环境中的Layers面板

    Layers(分层)下拉列表:用来控制Scene视图中游戏对象的显示,在下拉菜单中为勾选状态的物体将显示在Scene视图中. Everything:显示所有的游戏对象 Nothing:不显示任何游戏对 ...

  9. Investigation of Different Nets and Layers

    Investigation of Different Nets and Layers Overview of AlexNet (MIT Places | Flickr Finetuned | Oxfo ...

随机推荐

  1. 【BZOJ 2618】 2618: [Cqoi2006]凸多边形 (半平面交)

    2618: [Cqoi2006]凸多边形 Description 逆时针给出n个凸多边形的顶点坐标,求它们交的面积.例如n=2时,两个凸多边形如下图: 则相交部分的面积为5.233. Input 第一 ...

  2. request重定向或者是response转发请求后面的代码依然执行

    调用response.redirect(),或者request.getRequestDispatcher(loginAddr).forward(request,response);后,后面的代码照样执 ...

  3. ajax readyState的五种状态详解

    通过ajax的readyState的值,我们可以知道当前的这个http请求处于什么状态.对于web的调试是比较重要的. readyState 状态说明: (0)未初始化 此阶段确认XMLHttpReq ...

  4. No bean named 'transactionManager' is defined

    2016-10-20 23:27:17.771 INFO 7096 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped &quo ...

  5. python学习笔记三--字典的使用

    一.基本使用: 1. 赋值:{key:value} 1.1 与列表相同处:会改变索引(键)相关联的值的改变 1.2 与列表不同处:不用考虑值的长度,而列表是有序的需要考虑末尾偏移量,超过末尾偏移量的会 ...

  6. poj 3687 Labeling Balls(拓扑排序)

    题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...

  7. hdu 1874 畅通工程续(模板题 spfa floyd)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1874 spfa 模板 #include<iostream> #include<stdio ...

  8. struts2的action中获得request response session 对象

    在struts2中有两种方式可以得到这些对象 1.非IoC方式 要获得上述对象,关键Struts 2中com.opensymphony.xwork2.ActionContext类.我们可以通过它的静态 ...

  9. .NET下文本相似度算法余弦定理和SimHash浅析及应用

    余弦相似性 原理:首先我们先把两段文本分词,列出来所有单词,其次我们计算每个词语的词频,最后把词语转换为向量,这样我们就只需要计算两个向量的相似程度.   我们简单表述如下   文本1:我/爱/北京/ ...

  10. POJ (线段相交 最短路) The Doors

    题意: 一个正方形中有n道竖直的墙,每道墙上开两个门.求从左边中点走到右边中点的最短距离. 分析: 以起点终点和每个门的两个端点建图,如果两个点可以直接相连(即不会被墙挡住),则权值为两点间的欧几里得 ...