您的位置:首页 > 移动开发 > IOS开发

iOS-对CALayer的最简单使用

2014-03-19 15:11 218 查看
iOS的Core Animation,有很多概念,包括Layer(层)、Animation(动画)和Timing(时序)、Layout(布局)和constraint(约束)、Transaction(处理)。其中Layer是最基础的。

本文示例将在层中显示图片,层的背景是黑色的,层被定位在屏幕的中心,可以通过背景辨认出哪部分是由层来实现的。





Layer有多种,最基本的是CALayer,它也是其他种类Layer的父类。CALayer的子类有:

CAScrollLayer,用于简化显示层的一部分
CATextLayer,便于从字符串生成内容是文本的层
CATiledLayer,可用于显示复杂的图片
CAOpenGLLayer,提供OpenGLES渲染环境

Core Animation是基于QuartzCore的,需要在项目中引入框架:





另外,要在文件中import:

#import

代码如下:

- (void)viewDidLoad {
//取消状态栏,并有个动画效果
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide];
//按照屏幕大小生成UIview
self.view=[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
//设置背景颜色
self.view.backgroundColor=[UIColor blueColor];

//[self loadImageViews];
[self loadSimpleImageAnimation];

}

-(void)loadSimpleImageAnimation{
//创建图片对象
UIImage *image = [UIImage imageNamed:@"6.png"];
//创建层
CALayer *layer = [CALayer layer];
layer.backgroundColor = [[UIColor blackColor] CGColor];//设置背景色
layer.bounds = CGRectMake(0, 0, image.size.width,image.size.height);//层设置为图片大小
layer.contents = (id)[image CGImage];//层的内容设置为图片
layer.position = CGPointMake(1024/2 , 768/2);//层在view的位置
[self.view.layer addSublayer:layer];//将层加到当前View的默认layer下

[layer release];
[image release];
}


原文链接:http://marshal.easymorse.com/archives/3966
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: