CALayer, CoreGraphics与CABasicAnimation介绍
今天我们来看一下CALayer、CoreGraphics和CABasicAnimation。这些东西在处理界面绘制、动画效果上非常有用。
本篇博文就讲介绍CALayer的基本概念,使用CoreGraphics自定义绘制,以及基于CABasicAnimation的动画。以下内容都假定您有一定的Object-C基础,也熟悉UIView等相关的操作。如果不熟的话,您还要自行查阅资料。这里就不多讲了。
要使用CALayer,首先要添加QuartzCore框架。然后在你的Controller里添加包含该框架的头文件
#import <QuartzCore/QuartzCore.h>
在Controller的实现中添加viewDidAppear:方法。
每个view都有layer对象。可以通过view的layer属性访问。也可以创建一个layer对象:
CALayer *layer = [CALayer layer];
默认的,layer的frame是CGRectZero。虽然默认的添加了之后(addSublayer)看不见,但是layer已经存在了。为了让这个layer现实出来,修改一下这些可视属性。
layer.frame = CGRectMake(, , , );
layer.backgroundColor = [UIColor orangeColor].CGColor;
这里layer接受的是color的CGColor属性值。添加到controller的view.layer的子layer中:
[self.view.layer addSublayer:layer];
运行起来项目,就可以看到这个orange色的一片。那就是你刚刚添加的layer。
现在开始研究自定义绘制。开始在layer上绘制之前需要给layer设置代理
[layer setDelegate:self];
注意:layer的代理如果是Controller的话,没有什么问题。如果是CAlayer或者UIView及其子类的话就会出问题。当自定义绘制时,会调用这个方法
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx{
NSLog(@"layer custom draw");
}
但是运行项目你会发现这个方法不会自动被调用,在Console里不会打印出“layer custom draw”。很有意思啊,这是为什么呢?这是因为我们需要自己调用触发layer绘制的方法。很简单!咱们已经设定好了代理,那么只需要调用这个方法
[layer setNeedsDisplay];
那么这时你再运行起来项目看看,Console就会打印出“layer custom draw”这个字符串了。
把下面的代码复制到你的绘制方法里。看看CoreGraphics是如何起作用的。运行效果,如图:
大家可以看到一条黑线,额,黑线,贯穿layer。
这个layer看起来太方了,来个圆角是不是更好。再加个边条,加个阴影。
layer.cornerRadius = ;
layer.borderColor = [UIColor yellowColor].CGColor;
layer.borderWidth = ;
layer.shadowColor = [UIColor blackColor].CGColor;
layer.shadowOffset = CGSizeMake(, );
layer.shadowOpacity = .8f;
跑起来代码,看看效果:
下面来看看CABasicAnimation。
在viewDidAppear方法中,添加完layer之后添加动画的代码:
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
[animation setDuration:1.0];
[animation setRepeatCount:INT_MAX];
[animation setFromValue:[NSNumber numberWithFloat:0.0]];
[animation setToValue:[NSNumber numberWithFloat:1.0]];
[layer addAnimation:animation forKey:nil];
跑起来看看,对于layer透明度动画在视图加载完之后动画开始重复播放1000次。
全部代码(略有更改)
#import "ADImplicitViewController.h"
#import <QuartzCore/QuartzCore.h> @interface ADImplicitViewController ()
@property (nonatomic, weak) CALayer *animLayer;
@end @implementation ADImplicitViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
} - (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear: animated];
self.view.backgroundColor = [UIColor whiteColor]; CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(, , , );
layer.backgroundColor = [UIColor orangeColor].CGColor;
_animLayer = layer;
[layer setDelegate:self]; [self.view.layer addSublayer:layer]; // [layer setNeedsDisplay]; layer.cornerRadius = ;
layer.borderColor = [UIColor yellowColor].CGColor;
layer.borderWidth = ;
layer.shadowColor = [UIColor blackColor].CGColor;
layer.shadowOffset = CGSizeMake(, );
layer.shadowOpacity = .8f; CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
[animation setDuration:1.0];
[animation setRepeatCount:];
[animation setFromValue:[NSNumber numberWithFloat:0.0]];
[animation setToValue:[NSNumber numberWithFloat:1.0]];
[layer addAnimation:animation forKey:nil];
} - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx{
NSLog(@"layer custom draw"); // CGContextSetStrokeColorWithColor(ctx, [UIColor blackColor].CGColor);
// CGContextSetLineWidth(ctx, 5);
//
// CGContextMoveToPoint(ctx, 5, 5);
// CGContextAddLineToPoint(ctx, 95, 95);
//
// CGContextStrokePath(ctx);
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (IBAction)startAction:(id)sender { _animLayer.backgroundColor = [UIColor redColor].CGColor;
} @end
CALayer, CoreGraphics与CABasicAnimation介绍的更多相关文章
- CALayer图层的基本介绍
掌握 ● CALayer的基本属性 ● CALayer和UIView的关系 ● position和anchorPoint的作⽤用 CALayer ● 在iOS中,你能看得见摸得着的东西基本上都是UIV ...
- POP动画引擎中Layer与CALayer的一点区别
POP动画引擎是facebook提供的一个开源框架, 可以实现很多的动画效果, 这里就不一一介绍啦, 有兴趣的童鞋请移步: https://github.com/facebook/pop 下面简单的讲 ...
- 25个增强iOS应用程序性能的提示和技巧--中级篇
25个增强iOS应用程序性能的提示和技巧--中级篇 标签: ios性能优化内存管理 2013-12-13 10:55 738人阅读 评论(0) 收藏 举报 分类: IPhone开发高级系列(34) ...
- 25 个增强iOS应用程序性能的提示和技巧 应用程序性能的提示和技巧
初级 在开发过程中,下面这些初级技巧需要时刻注意: 1.使用ARC进行内存管理2.在适当的情况下使用reuseIdentifier3.尽可能将View设置为不透明(Opaque)4.避免臃肿的XIBs ...
- 增强iOS应用程序性能的提示和技巧(25个)
转自 http://www.cocoachina.com/newbie/basic/2013/0522/6259.html 在开发iOS应用程序时,让程序具有良好的性能是非常关键的.这也是用户所期望的 ...
- (转)25个增强iOS应用程序性能的提示和技巧--中级篇
在性能优化时,当你碰到一些复杂的问题,应该注意和使用如下技巧: 9.重用和延迟加载View10.缓存.缓存.缓存11.考虑绘制12.处理内存警告13.重用花销很大的对象14.使用Sprite Shee ...
- 25个增强iOS应用程序性能的提示和技巧 — 中级篇
本文由破船译自:raywenderlich 转载请注明出处:BeyondVincent的博客 _____________ 在开发iOS应用程序时.让程序具有良好的性能是非常关键的.这也是用户所期望的. ...
- iOS简单动画
知识架构 CALayer 图层类 CABasicAnimation 基础动画 CAKeyFrameAnimation 帧动画 CATransition 转场动画 CAAnimationGroup 动画 ...
- IOS Animation-动画基础、深入
1. Model Layer Tree(模型层树)和Presentation Layer Tree(表示层树) CALayer是动画产生的地方.当我们动画添加到Layer时,是不直接修改layer的属 ...
随机推荐
- 嵌入式Linux驱动和固件有何区别?供应商是如何用固件压缩成本的?
作为一个驱动开发者, 你可能发现你面对一个设备必须在它能支持工作前下载固件到它里面. 硬件市场的许多地方的竞争是如此得强烈, 以至于甚至一点用作设备控制固件的 EEPROM 的成本制造商都不愿意花费. ...
- 初识python(python的安装与运行)
python--“优雅”.“明确”.“简单”的哲学定位 一.python的安装(Windows环境下) 1.在python官网下载安装文件 python的官方网址:https://www.python ...
- PHP错误日志记录文件位置确定
1.确定web服务器 ( IIS, APACHE, NGINX 等) 以哪一种方式支持PHP,通常是有下面2种方式 通过模块加载的方式, 适用于apache 通过 CGI/fastCGI 模式, 该模 ...
- EDAS字体嵌入问题解决方法
提交IEEE EDAS文章时出现:“The paper PDF file cannot be accepted: Publishers require that PDF fonts are embed ...
- vue+uwsgi+nginx部署项目
首先先下载好前后端项目 先从前端vue搞起 要在服务器上,编译打包vue项目,必须得有node环境 下载node二进制包,此包已经包含node,不需要再编译 wget https://nodejs.o ...
- 使用PowerMap生成地图图文
地区 GDP(亿)广东省 80854江苏省 77388山东省 68024浙江省 47251河南省 40471四川省 32934湖北省 32665河北省 ...
- 设置VS2010和IE8 调试ATL控件<转>
VS2010设置篇: 设置VS2010 通过IE8 调试ATL的ActiveX控件的步骤如下: 设置项目属性 在项目的属性页对话框中选中“配置属性—>调试”,在右边填入下列参数: 命令 ...
- Android MVP模式简单易懂的介绍方式 (三)
Android MVP模式简单易懂的介绍方式 (一) Android MVP模式简单易懂的介绍方式 (二) Android MVP模式简单易懂的介绍方式 (三) 讲完M和P,接下来就要讲V了.View ...
- Tornado 多进程 & 异步
另外一篇:http://www.cnblogs.com/xiaoshi657/p/6945208.html 基本版: #coding=utf-8 import tornado.web import t ...
- javascript常见方法汇总之一——数组字符串相关
(转载至慕课网) 原文链接:https://www.imooc.com/article/46933 github地址:https://github.com/dorseysen/notes-about- ...