Core Animation中的基础动画

基础动画

在开发过程中很多情况下通过基础动画就可以满足开发需求,前面例子中使用的UIView代码块进行图像放大缩小的演示动画也是基础动画(在iOS7 中UIView也对关键帧动画进行了封装),只是UIView装饰方法隐藏了更多的细节。如果不使用UIView封装的方法,动画创建一般分为以下几步:

1.初始化动画并设置动画属性

2.设置动画属性初始值(可以省略)、结束值以及其他动画属性

3.给图层添加动画

//
// ViewController.m
// Demo0218
//
// Created by wiseman on 16/2/18.
// Copyright (c) 2016年 wiseman. All rights reserved.
// #import "ViewController.h" @interface ViewController () {
CALayer *_layer;
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self test1];
} -(void)test1{
//CA核心动画:基础动画,关键帧动画,动画组
_layer = [[CALayer alloc]init];
_layer.bounds = CGRectMake(, , , );
_layer.position = CGPointMake(, );
_layer.backgroundColor = [UIColor redColor].CGColor;
[self.view.layer addSublayer:_layer]; } //移动动画
-(void)translatonAnimation:(CGPoint)location{
//创建动画并制定动画属性
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
//设置动画属性的初始值和结束值
// basicAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
basicAnimation.toValue = [NSValue valueWithCGPoint:location];
basicAnimation.duration = 2.0;
//运行一次是否删除动画
// basicAnimation.removedOnCompletion = NO;
//设置重复次数,HUGE_VALF可看做无穷大,起到循环动画的效果
// basicAnimation.repeatCount = HUGE_VALF; //代理
basicAnimation.delegate = self;
[basicAnimation setValue:[NSValue valueWithCGPoint:location] forKey:@"KCBasicAnimationLocation"];
[basicAnimation setValue:@"fuck1" forKey:@"AnimName"]; //添加动画到图层,注意key相当于给动画进行命名,以后获得该动画时可以使用此名称获取
[_layer addAnimation:basicAnimation forKey:@"fuck"];
} //旋转动画
-(void)rotationAnimation{
//创建动画并制定动画属性
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
//设置动画属性的初始值和结束值
// basicAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
basicAnimation.toValue=[NSNumber numberWithFloat:M_PI_2*];
basicAnimation.duration = 3.0;
// basicAnimation.autoreverses = true;//旋转后再旋转到原来的位置
basicAnimation.repeatCount = HUGE_VALF; //添加动画到图层,注意key相当于给动画进行命名,以后获得该动画时可以使用此名称获取
[_layer addAnimation:basicAnimation forKey:@"fuck2"];
} -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = touches.anyObject;
CGPoint location = [touch locationInView:self.view];
//创建并开始动画
[self translatonAnimation:location];
[self rotationAnimation];
} - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{ NSString *str = [anim valueForKey:@"AnimName"]; if ([str isEqualToString:@"fuck1"]) {
CGPoint point = [[anim valueForKey:@"KCBasicAnimationLocation"] CGPointValue];
//开启事务
[CATransaction begin];
//禁用隐式动画
[CATransaction setDisableActions:YES]; _layer.position = point; //提交事务
[CATransaction commit];
} } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
上一篇:转:Linux(Centos)之安装Nginx及注意事项


下一篇:基于jQuery很牛X的批量上传插件