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的属 ...
随机推荐
- ALSA声卡12_从零编写之添加音量控制_学习笔记
1.设置音量时应用程序的调用过程 (1)strace分析: amixer cset numid=1 30 (设置音量) /dev/snd/controlC0 open SNDRV_CTL_IOCTL_ ...
- 5月3日上课笔记-properties文件,junit测试,mvc封层等
StringBuffer 线程安全,效率低 StringBuilder 线程不安全,效率高 判断数组是null还是空数组 null 空数组 int[] array=null; int[] array2 ...
- 优化笔记: jxrsfxrxx_D_20140916.gz
表的重复扫描. ----------------------------------想进一步研究,继续往下看------------------------------ 1. 所有相似 ...
- MySQL多实例介绍
我们前面已经做了MySQL数据库的介绍以及为什么选择MySQL数据库,最后介绍了MySQL数据库在Linux系统下的多种安装方式,以及讲解了MySQL的二进制方式单实例安装.基础优化等,下面给大家讲解 ...
- C#实现并口输出输入高低电位
PC并行口各阵脚定义: 1.选通,PC->Printer 2-9 数据(D0-D7) 10.应答(ACK),Printer->PC 11.忙(BUSY),Printer->PC 12 ...
- Spring Test 整合 JUnit 4 使用总结
转自:https://blog.csdn.net/hgffhh/article/details/83712924 这两天做Web开发,发现通过spring进行对象管理之后,做测试变得复杂了.因为所有的 ...
- c++Builder Delphi XML 解析例子
XMLDocument; Xml.XMLIntf.hpp Xml.XMLDoc.hpp #include "Xml.Win.msxmldom.hpp" XMLDocument控件 ...
- 跟着太白老师学python day11 可迭代对象和迭代器
如果对象的属性中有__iter__属性就说明是可迭代的,容器类的数据类型都是可迭代对象 如果对象的属性中既有__iter__属性也有__next__属性,就说明这个对象是迭代器 如何判断一个函数是不是 ...
- C# 6 与 .NET Core 1.0 高级编程 - C# 6 的新功能
个人原创译文,转载请注明出处.有不对的地方欢迎指出与交流. 英文原文:Professional C# 6 and .NET Core 1.0 - What’s New in C# 6 C# 6 的新功 ...
- python:数组/列表(remove()函数、append()函数、sort()函数、reverse()函数)
排序: 1:整理顺序 #冒泡 lista = [5,7,11,19,99,63,3,9,1] list = [] while lista != []: number = 0 for i in list ...