今天我们来看一下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介绍的更多相关文章

  1. CALayer图层的基本介绍

    掌握 ● CALayer的基本属性 ● CALayer和UIView的关系 ● position和anchorPoint的作⽤用 CALayer ● 在iOS中,你能看得见摸得着的东西基本上都是UIV ...

  2. POP动画引擎中Layer与CALayer的一点区别

    POP动画引擎是facebook提供的一个开源框架, 可以实现很多的动画效果, 这里就不一一介绍啦, 有兴趣的童鞋请移步: https://github.com/facebook/pop 下面简单的讲 ...

  3. 25个增强iOS应用程序性能的提示和技巧--中级篇

    25个增强iOS应用程序性能的提示和技巧--中级篇 标签: ios性能优化内存管理 2013-12-13 10:55 738人阅读 评论(0) 收藏 举报  分类: IPhone开发高级系列(34)  ...

  4. 25 个增强iOS应用程序性能的提示和技巧 应用程序性能的提示和技巧

    初级 在开发过程中,下面这些初级技巧需要时刻注意: 1.使用ARC进行内存管理2.在适当的情况下使用reuseIdentifier3.尽可能将View设置为不透明(Opaque)4.避免臃肿的XIBs ...

  5. 增强iOS应用程序性能的提示和技巧(25个)

    转自 http://www.cocoachina.com/newbie/basic/2013/0522/6259.html 在开发iOS应用程序时,让程序具有良好的性能是非常关键的.这也是用户所期望的 ...

  6. (转)25个增强iOS应用程序性能的提示和技巧--中级篇

    在性能优化时,当你碰到一些复杂的问题,应该注意和使用如下技巧: 9.重用和延迟加载View10.缓存.缓存.缓存11.考虑绘制12.处理内存警告13.重用花销很大的对象14.使用Sprite Shee ...

  7. 25个增强iOS应用程序性能的提示和技巧 — 中级篇

    本文由破船译自:raywenderlich 转载请注明出处:BeyondVincent的博客 _____________ 在开发iOS应用程序时.让程序具有良好的性能是非常关键的.这也是用户所期望的. ...

  8. iOS简单动画

    知识架构 CALayer 图层类 CABasicAnimation 基础动画 CAKeyFrameAnimation 帧动画 CATransition 转场动画 CAAnimationGroup 动画 ...

  9. IOS Animation-动画基础、深入

    1. Model Layer Tree(模型层树)和Presentation Layer Tree(表示层树) CALayer是动画产生的地方.当我们动画添加到Layer时,是不直接修改layer的属 ...

随机推荐

  1. canvas之太阳系效果

    星球 变量名 公转周期 光色 暗色 水星 Mercury 87.70 #a69697 #5c3e40 金星 Venus 224.701.70 #c4bbac #1f1315 地球 Earth 365. ...

  2. MariaDB主从异步复制详解

    一 异步复制(Asynchronous replication) 1.MariaDB本身支持单向的.异步的复制.异步复制意味着在把数据从一台机器拷贝到另一台机器时有一个延时,最重要的是这意味着当应用系 ...

  3. spring aop实现拦截接口请求打印日志

    在spring配置 1编写自己的注解类 2.编写注解解析类 3.配置spring aop代理 (下面我使用注解 如使用配置 配置切点即可,有两种代理默认jdk代理 设置true 为cglib代理) / ...

  4. App压力测试背景

    开展压力测试 原因: 提高产品的稳定性 提高产品的留存率 时间: 首轮功能测试通过 夜间进行(将工具设置后进行,节约资源) 如何开展: 确定事件流 模拟事件流

  5. win8下ctrl+alt+down失效问题

    最近换win8系统后用myeclipse发现这个用得最多快捷键居然不能用了. 百度后的确是显卡的快捷键冲突,但禁用显卡快捷键后仍然无效,把eclipse换成其他的不能忍. 所以把显卡快捷键换了就可以了 ...

  6. Rhythmk 一步一步学 JAVA(11)Ibatis 环境配置

    1.项目文件分布. 2.example1.java: package com.rhythmk.example1; import java.io.IOException; import java.io. ...

  7. python学习——练习题(12)

    """ 题目:判断101-200之间有多少个素数,并输出所有素数. 质数(prime number)又称素数,有无限个. 质数定义为在大于1的自然数中,除了1和它本身以外 ...

  8. vue -Missing space before value for key 'path'vue.js解决空格报错

    webpack.base.config.js文件注释掉下面的东西!! module: { rules: [      /*{        test: /\.(js|vue)$/,        lo ...

  9. 删除链表之two star programming

    最近偶尔看到一篇关于Linus Torvalds的访问,大神说大部分人都不理解指针.假设被要求写一段链表删除节点的程序,大多数的做法都是跟踪两个指针,当前指针cur和其父节点pre,这种实现很容易理解 ...

  10. Mysql双机热备实现数据库高可用

    mysql双主热备,也称主主互备,目的是mysql数据库高可用,只支持双机,原因是mysql的复制是一主多从,但一个从服务器只能有一个主服务器. 双机热备的条件是双机mysql版本必须一致. 服务器分 ...