进入iOS图形领域一个非常重要的QuartzCore,先看看这个库有什么
当然从最重要但是最陌生的CALayer入手了,在本文章结尾都是学习过程中参考到的一些重要的好文章的来源。
参考文顶顶大神的文章就可以知道,通过操作CALayer很容易去修改显示出来的一些颜色,边框,背景,阴影等图形的属性。
例:
- (void)viewDidLoad
{
[super viewDidLoad];
self.vview = [[UIView alloc] init];
[self.vview setFrame:CGRectMake(0, 0, 150, 150)];
[self.view addSubview:self.vview];
self.vview.layer.borderWidth = 20;
self.vview.layer.borderColor = [UIColor greenColor].CGColor;
self.vview.layer.cornerRadius = 15.0;
}
结果:
注意到CALayer的一个属性:
/* An object providing the contents of the layer, typically a CGImageRef,
* but may be something else. (For example, NSImage objects are
* supported on Mac OS X 10.6 and later.) Default value is nil.
* Animatable. */
@property(nullable, strong) id contents;
这个属性表示它的接受内容,通常是CGImageRef,这个类型是一个封装了位图信息的结构体,在CoreGraphics库所定义的在学习截屏的时候有用过。
意味着可以直接设置图层的属性达到显示图片的作用。
添加一句代码:
self.vview.layer.contents = (id)[UIImage imageNamed:@"loading_monkey"].CGImage;
这里用文顶顶大神的图,因为要讲解一下为什么会出现图中死角出现的令人讨厌的白色部分:
同样,原因大神已经透析得非常好了:
“那是因为设置的image不是展示在主图层上的,而是显示在子图层上的。可以通过设置一个范围,设置超出主图层的部分把它给剪切掉”
稍稍解释一下,UIView设置根layer成带有一点园角的框后,这个时候设置layer的contents还是还是根据UIView容器的大小来设置的,而显示出的内容却则被由图层显示的框框所界定出内外两部分,所以就显示出四个令人讨厌的角了。
UIView存在一个方法:
@property(nonatomic) BOOL clipsToBounds; // When YES, content and subviews are clipped to the bounds of the view. Default is NO.
只要设置这个属性就可以解决了,下篇章我们来看看如何用图层的方法来解决。
1.文顶顶学习三部曲:
文顶顶:
(1).Postion anchorPoint :
http://www.cnblogs.com/wendingding/p/3800736.html
(2).基本使用:
http://www.cnblogs.com/wendingding/p/3800010.html
http://www.cnblogs.com/wendingding/p/3800652.html
(3).自定义:
http://www.cnblogs.com/wendingding/p/3800961.html
2.UIView区别与联系:
UIView CALayer 区别:
功能实现上:
http://www.cocoachina.com/ios/20150828/13244.html
http://www.jianshu.com/p/079e5cf0f014
有了区别还要有关系:
http://www.cnblogs.com/yswdarren/p/3555436.html
3.设计思想上:
http://www.cocoachina.com/ios/20150828/13257.html
引申:
http://blog.csdn.net/shandianling/article/details/8446647