//
// ViewController.m
// core
//
// Created by chenvinci on 2017/2/16.
// Copyright © 2017年 cuijing. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *redView;
@property (weak, nonatomic) IBOutlet UIImageView *img;
@property(nonatomic) int iIndex;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.iIndex = 0;
self.redView.layer.anchorPoint = CGPointMake(0, 0);
self.redView.layer.position = CGPointMake(100, 100);
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//平移动画
CABasicAnimation * moveA = [[CABasicAnimation alloc]init];
moveA.keyPath = @"position";
moveA.byValue = [NSValue valueWithCGPoint:CGPointMake(50, 50)];//每次移动(50,50)
//bounds动画
CABasicAnimation * boundA = [CABasicAnimation animation];
boundA.keyPath = @"bounds";
boundA.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 40, 40)];
//创建组动画
CAAnimationGroup*group = [CAAnimationGroup animation];
group.animations = @[moveA,boundA];
group.duration = 3;
//动画完成时不动
group.removedOnCompletion = NO;
group.fillMode = kCAFillModeForwards;
//添加动画
[self.img.layer addAnimation:group forKey:nil];
}
-(void)touchesBegan3:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//创建转场动画
CATransition* animation1 = [CATransition animation];
NSArray* arr = @[@"cube",@"moveIn",@"reveal",@"pageCurl",@"suckEffect",@"rippleEffect",@"oglFlip"];
if (self.iIndex >= arr.count) {
self.iIndex = 0;
}
animation1.type = arr[self.iIndex];
animation1.subtype = @"fromTop";
self.iIndex++;
//添加动画
[self.img.layer addAnimation:animation1 forKey:nil];
}
-(void)touchesBegan2:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//创建关键帧动画
CAKeyframeAnimation*animation1 = [[CAKeyframeAnimation alloc]init];
animation1.duration = 4;
//设置动画属性 修改position
animation1.keyPath = @"position";
NSValue*v1 = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
NSValue*v2 = [NSValue valueWithCGPoint:CGPointMake(100, 0)];
NSValue*v3 = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
NSValue*v4 = [NSValue valueWithCGPoint:CGPointMake(0, 100)];
NSValue*v5 = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
animation1.values = @[v1,v2,v3,v4,v5];
//设置各关键帧的时间
NSNumber*t1 = [NSNumber numberWithFloat:0.1];//最大是1
NSNumber*t2 = [NSNumber numberWithFloat:0.2];
NSNumber*t3 = [NSNumber numberWithFloat:0.8];
NSNumber*t4 = [NSNumber numberWithFloat:0.9];
NSNumber*t5 = [NSNumber numberWithFloat:1.0];
animation1.keyTimes = @[t1,t2,t3,t4,t5];
//动画执行完以后是否移除?
animation1.removedOnCompletion = NO;
animation1.fillMode = kCAFillModeForwards;//保持最新状态
// 添加动画
[self.redView.layer addAnimation:animation1 forKey:nil];
}
-(void)touchesBegan1:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//创建基础动画
CABasicAnimation* animation1 = [[CABasicAnimation alloc]init];
//设置动画属性 修改bounds
animation1.keyPath = @"bounds";//改bounds属性要去掉autolayout
animation1.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 100, 100)];
animation1.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];
//设置动画属性 修改position
animation1.keyPath = @"position";
animation1.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
animation1.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
//设置动画属性 修改transform
animation1.keyPath = @"transform.translation.x";
animation1.fromValue = [NSNumber numberWithInt:20];
animation1.toValue = [NSNumber numberWithInt:50];
//设置代理
animation1.delegate = self;
//设置时长
animation1.duration = 2;
//动画执行完以后是否移除?
animation1.removedOnCompletion = NO;
animation1.fillMode = kCAFillModeForwards;//保持最新状态
// 添加动画
[self.redView.layer addAnimation:animation1 forKey:nil];
}
- (void)animationDidStart:(CAAnimation *)anim{
NSLog(@"start");
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
NSLog(@"stop");
}
@end
核心动画
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 参考:第三篇:iOS动画系列之三:Core Animation。介绍了Core Animation的常用属性和方法...
- 每个人的心中都住着两只狼,一只善,一只恶。如果说走向心智成熟的旅程是善,那么与成长背道而驰,迷失在谎言的陷阱里就是...