今天我们来看一下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. python‘s third day for me 字符串方法

    基 础 数 据 类 型 初 始   int  运算.+  -  *  /  **  %... bool: 判断,真假,作为条件. str:  存储少量的数据.操作简单,便于传输. list:  列表[ ...

  2. java 红包规则

    java 红包规则 拼手气红包: 规则:最大金额:全部金额/个数*倍数 最小金额:0.01 最后一个红包是全部金额-领取金额 随机分配 package com.utils; import java.m ...

  3. Chrome Postman及Firefox Poster使用

    Chrome浏览器跟Postman工具共用代理设置及Cookie Firefox浏览器跟Poster工具共用代理设置及Cookie   xdebug调试原理 第一次请求url通过参数XDEBUG_SE ...

  4. CentOS7|Redhat7挂载NTFS格式磁盘

    //下载安装ntfs-3g_ntfsprogs.tgz软件包进行编译安装 tar -zxf ntfs-3g_ntfsprogs.tgz cd ntfs-3g_ntfsprogs ./configure ...

  5. halcon-车牌识别

    halcon代码: 1: read_image(Image,'D:/MyFile/halcon/车牌识别/图片.jpg') 2: decompose3(Image,ImageR,ImageG,Imag ...

  6. linux 常用查看设备命令(转)

    # uname -a # 查看内核/操作系统/CPU信息 # /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # hostname # 查看计算 ...

  7. unity2017.1.0f3与旧的粒子系统不兼容

    在测试旧版本插件unistorm时用unity2017.1.0f3打开后其它天气效果显示正常,雨点看不到,再用unity5.52打开后,所有效果都可以看到了. 记录备忘

  8. objective-C 的内存管理之-实例分析

    objective-C 的内存管理之-实例分析 注:这是<Objective-C基础教程>一书上的实例,但是原书限于篇幅,分析得比较简单,初次阅读看得比较费劲,这里展开详细讨论一下. 场景 ...

  9. C# 调用动态代码

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. Linux实战教学笔记47:JAVA企业级应用服务器之TOMCAT实战

    第一章 Tomcat简介 Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta项目中的一个核心项目,由Apache,Sun和其他一些公司及个人共 ...