@interface secondViewController ()
@property (nonatomic,strong) UIView *redView;
@property (nonatomic,strong) UIView *greenView;
@end
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = @"View动画";
[self.view addSubview:self.greenView];
[self.view addSubview:self.redView];
#button 的点击方法
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setFrame:CGRectMake(200,400, 100, 50)];
[button setTitle:@"动画" forState: UIControlStateNormal];
[button addTarget:self action:@selector(groupAnimation) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
[.m]文件夹下
basicAnimation的学习
#pragma mark----basicAnimation的学习
- (CABasicAnimation *)basicAnimation{
//keyPath:填写动画效果所需要改变的属性名称;
CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];//transform.scale整体缩放.
//在动画结束之后是否移除动画效果.
// basic.removedOnCompletion = NO;
//动画结束之后的状态设置
// basic.fillMode = kCAFillModeForwards;
//动画时长.
// basic.duration = 0.2;
//动画重复次数.
// basic.repeatCount = MAXFLOAT;
//动画是否反转
// basic.autoreverses = YES;
//是原来的二倍;
basic.byValue = @(M_PI_2 * 6.0);
//把 layer 层所有的动画都删掉
// [self.greenView.layer removeAllAnimations];
// [self.greenView.layer removeAnimationForKey:@"basic"];
// basic.fromValue = [NSNumber numberWithFloat:0.0];
// basic.toValue = [NSNumber numberWithFloat:6.0 * M_PI_2];
// CABasicAnimation *basic1 = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
// basic1.removedOnCompletion = NO;
// basic1.fillMode = kCAFillModeForwards;
// basic1.duration = 0.8;
// basic1.repeatCount = MAXFLOAT;
// basic1.autoreverses = YES;
// basic1.byValue = @(100);
return basic;//在组合动画使用时,要将 return 写在 layer 的上方,以防止在操作时与组合动画出现重复操作
//将动画添加到 layer层.
//第一个参数;需要作用到 layer 层的动画. forkey 动画标记.删除动画的时候,可以根据标记删除.
[self.greenView.layer addAnimation:basic forKey:@"basic"];
[self.greenView.layer addAnimation:basic1 forKey:@"basic1"];
[self.redView.layer addAnimation:basic forKey:@"basic"];
[self.redView.layer addAnimation:basic1 forKey:@"basic1"];
// return basic;
}
帧动画
#pragma mark-----帧动画
- (CAKeyframeAnimation*)keyFromAnimation{
//position layer的信息位置,类似于 view 的 center 点.
CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//layer 最终的position值
float position_x = self.greenView.layer.position.x;
float position_y = self.greenView.layer.position.y;
//想做晃动的偏移量;
NSValue *leftValue = [NSValue valueWithCGPoint: CGPointMake(position_x - 50, position_y)];
//layer的原始位置
NSValue *originValue = [NSValue valueWithCGPoint:CGPointMake(position_x, position_y)];
//layer 向右晃动的位置
NSValue *rightVaue = [NSValue valueWithCGPoint:CGPointMake(position_x + 50, position_y)];
//添加每一帧的 value值
[keyFrame setValues:@[originValue,leftValue,originValue,rightVaue,originValue]];
// keyFrame.repeatCount = 20;
// keyFrame.repeatDuration = 3.0;
return keyFrame;
//为layer 层添加动画
// [self.greenView.layer addAnimation:keyFrame forKey:@"aaa"];
}
组合动画
#pragma mark----组合动画
- (void)groupAnimation{
//初始化动画
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
//basic
CABasicAnimation *basic = [self basicAnimation];
CAKeyframeAnimation *keyFrame = [self keyFromAnimation];
//将得到的动画放入 group 中
animationGroup.animations = @[basic,keyFrame];
// animationGroup.repeatCount = 1000;
animationGroup.repeatDuration = MAXFLOAT;
//将 group 加到 layer 上.
[self.greenView.layer addAnimation:animationGroup forKey:@"group"];
}