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的属 ...
随机推荐
- OD 实验(四) - 去除 NAG 窗口的几种方法
程序: 运行 弹出一个窗口,说要注册 点击确定,到主窗口 关闭主窗口 然后弹出提醒注册的对话框 逆向程序 用 OD 打开程序 GetModuleHandleA 获取程序模块的句柄,程序在内存中的基址 ...
- sql server 2008 链接到数据库引擎
- Protobuf3教程
Protobuf3教程 https://blog.csdn.net/hulinku/article/details/80827018 Protobuf语言指南——.proto文件语法详解 https: ...
- 【C++】
C++声明function后面加上等于0(=0)何解? https://zhidao.baidu.com/question/1446181256925153340.html
- 前端学习---JavaScript
JavaScript基本知识 JavaScript是一门独立的语言,像我们学习php,python等需要安装apache,python3.6,那我们学习JavaScript只需要我们电脑有一个浏览器即 ...
- java第三方类库实现图片等比缩放
public class ThumbnailTest { public static void main(String[] args) { InputStream is = null; try { / ...
- Java并发编程之——Amino框架
Amino框架是一个采用无锁方式实现并行计算的框架,可惜的是,网上关于Amino框架的介绍甚少.根据所掌握的资料,稍微总结一下: 1. 锁机制到无锁机制 锁机制可以确保程序和数据的线程安全,但是锁是一 ...
- js添加对象数组
json 数组也是数组 var jsonstr="[{'name':'a','value':1},{'name':'b','value':2}]"; var jsonarray ...
- pid文件的作用
pid文件的作用 一.pid文件的作用 1.pid文件的内容用cat命令查看,可以看到内容只有一行,记录了该进程的ID 2.pid文件的作用防止启动多个进程副本 3.pid文件的原理进程运行后会给.p ...
- 让低版本IE支持Html5的新语义标签
HTML5能为我们做的事儿很多,最为可口的就是语义化标签的应用,如果你已经在Chrome或者其他支持HTML5的浏览器上用过它的牛x,那这篇文章对你一定有用,因为现在你也可以在IE上用到HTML5. ...