CALayer, CoreGraphics与CABasicAnimation介绍
来源:互联网 发布:js动态添加click事件 编辑:程序博客网 时间:2024/06/06 00:19
今天我们来看一下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(100, 100, 100, 100);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 = 20; layer.borderColor = [UIColor yellowColor].CGColor; layer.borderWidth = 5; layer.shadowColor = [UIColor blackColor].CGColor; layer.shadowOffset = CGSizeMake(10, 10); 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次。
全部代码(略有更改)
1 #import "ADImplicitViewController.h" 2 #import <QuartzCore/QuartzCore.h> 3 4 @interface ADImplicitViewController () 5 @property (nonatomic, weak) CALayer *animLayer; 6 @end 7 8 @implementation ADImplicitViewController 9 10 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil11 {12 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];13 if (self) {14 // Custom initialization15 }16 return self;17 }18 19 - (void)viewDidLoad20 {21 [super viewDidLoad];22 // Do any additional setup after loading the view.23 }24 25 - (void)viewDidAppear:(BOOL)animated{26 [super viewDidAppear: animated];27 self.view.backgroundColor = [UIColor whiteColor];28 29 CALayer *layer = [CALayer layer];30 layer.frame = CGRectMake(100, 100, 100, 100);31 layer.backgroundColor = [UIColor orangeColor].CGColor;32 _animLayer = layer;33 [layer setDelegate:self];34 35 [self.view.layer addSublayer:layer];36 37 // [layer setNeedsDisplay];38 39 layer.cornerRadius = 20;40 layer.borderColor = [UIColor yellowColor].CGColor;41 layer.borderWidth = 5;42 layer.shadowColor = [UIColor blackColor].CGColor;43 layer.shadowOffset = CGSizeMake(10, 10);44 layer.shadowOpacity = .8f;45 46 CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"opacity"];47 [animation setDuration:1.0];48 [animation setRepeatCount:1000];49 [animation setFromValue:[NSNumber numberWithFloat:0.0]];50 [animation setToValue:[NSNumber numberWithFloat:1.0]];51 [layer addAnimation:animation forKey:nil];52 }53 54 - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx{55 NSLog(@"layer custom draw");56 57 // CGContextSetStrokeColorWithColor(ctx, [UIColor blackColor].CGColor);58 // CGContextSetLineWidth(ctx, 5);59 // 60 // CGContextMoveToPoint(ctx, 5, 5);61 // CGContextAddLineToPoint(ctx, 95, 95);62 // 63 // CGContextStrokePath(ctx);64 }65 66 - (void)didReceiveMemoryWarning67 {68 [super didReceiveMemoryWarning];69 // Dispose of any resources that can be recreated.70 }71 72 - (IBAction)startAction:(id)sender {73 74 _animLayer.backgroundColor = [UIColor redColor].CGColor;75 }76 77 @end
- CALayer, CoreGraphics与CABasicAnimation介绍
- CALayer 与UIView介绍
- CABasicAnimation 介绍
- CABasicAnimation 介绍
- CABasicAnimation介绍
- iOS CoreGraphics 框架介绍
- UI 动画之CALayer+CAAnimation 之 CABasicAnimation
- CALayer介绍
- CALayer介绍
- CABasicAnimation的简单介绍
- CoreGraphics
- CoreGraphics
- CoreGraphics
- CoreGraphics
- CoreGraphics基本数据类型与函数
- CoreGraphics之CGContextSaveGState与UIGraphicsPushContext
- CGAffineTransform与CABasicAnimation
- 实现 圆形加载view 动画效果(用CALayer,CABasicAnimation实现)
- Android常用适配器分析(如何制作简易Launcher)
- spring与mybatis整合配置文件
- Return方法去除nag
- 从ArrayList中删除对象报java.util.ConcurrentModificationException异常解决
- 使用 hibernate 存取大对象数据类型(clob和blob)
- CALayer, CoreGraphics与CABasicAnimation介绍
- hdu 1874 畅通工程续 【Dijkstra Floyd-Warshall算法】
- 说说Zookeeper中的ACL
- Visual C++中min()和max()函数的使用
- 搭建JAVAEE Memcache 集群环境之与Spring集成(四)
- Android代码优化——使用Android lint工具
- 90后设计出来的手机病毒,确实震撼了不少人
- android:windowSoftInputMode属性使用
- su/sudo/id/whoami/who/w/passwd