标签:style blog class tar ext color
第一、综述
UIView的继承结构为:UIResponder:NSObject。可以看出UIView的直接父类为UIResponder类,.CALayer的继承结构:NSObject,直接从NSObject继承,因为缺少了UIResponder类,由上可见UIResponder是用来响应事件的,也就是说UIView可以响应用户事件,所以CALayer不能响应任何的用户事件,UIView是在/System/Library/Frameworks/UIKit.framework中定义的。苹果官方文档:
Core Animation doesn‘t provide a means for actually displaying layers in a window, they must be hosted by a view. When paired with a view, the view must provide event-handling for the underlying layers, while the layers provide display of the content.
The view system in iOS is built directly on top of Core Animation layers. Every instance of UIView automatically creates an instance of a CALayer class and sets it as the value of the view’s layer property. You can add sublayers to the view’s layer as needed.
On Mac OS X you must configure an NSView instance in such a way that it can host a layer
第三、属性
CALayer *subLayer=[CALayerlayer];
subLayer.frame=CGRectMake(80,140, 100, 100);
subLayer.backgroundColor=[UIColorredColor].CGColor;
subLayer.cornerRadius=15;
subLayer.shadowColor=[UIColorblueColor].CGColor;
subLayer.shadowOffset=CGSizeMake(0,2.0);
subLayer.shadowOpacity=1.0;
subLayer.borderColor=[UIColoryellowColor].CGColor;
subLayer.borderWidth=3.0;
[self.view.layeraddSublayer:subLayer];
截图:
第三、. masksToBounds : 很重要的属性,可以用此属性来防止子元素大小溢出父元素,如若防止溢出,设为 true
CALayer *subLayer=[CALayerlayer];
subLayer.frame=CGRectMake(80,140, 100, 100);
subLayer.cornerRadius=15;
subLayer.shadowColor=[UIColorblueColor].CGColor;
subLayer.shadowOffset=CGSizeMake(0,2.0);
subLayer.shadowOpacity=1.0;
subLayer.borderColor=[UIColoryellowColor].CGColor;
subLayer.borderWidth=3.0;
subLayer.contents=(id)[UIImageimageNamed:@"sample.jpg"].CGImage;
subLayer.masksToBounds=YES;
[self.view.layeraddSublayer:subLayer];
result:
标签:style blog class tar ext color
原文地址:http://blog.csdn.net/richard_rufeng/article/details/25112431