核心动画
// 所有的UIView 内部都有一个layer
// 我们所看到的view的外形 都是layer显示的
// 设置按钮内部图层的 圆角
btn.layer.cornerRadius = 50;
// 图层的边框颜色
// btn.layer.borderColor = [UIColor blackColor].CGColor;
// // 图层的边框宽度
// btn.layer.borderWidth = 5;
// 阴影颜色
btn.layer.shadowColor = [UIColor blackColor].CGColor;
// 阴影透明度
btn.layer.shadowOpacity = 1;
// 阴影偏移量
btn.layer.shadowOffset = CGSizeMake(5, 10);
// 阴影半径
btn.layer.shadowRadius = 7;
// 单独创建一个图层
CALayer *layer = [[CALayer alloc] init];
layer.frame = CGRectMake(100, 300, 100, 100);
layer.backgroundColor = [UIColor greenColor].CGColor;
[self.view.layer addSublayer:layer];
#pragma mark 转场动画
- (void)bntClick
{
CATransition * transition =[CATransition animation];
transition.duration = 1;
//rippleEffect 水波
transition.type = @"cube”;方块
transition.subtype = kCATransitionFromRight;
//两个view进行切换
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[self.view.layer addAnimation:transition forKey:@"1"];
}
事务动画
// 事务动画:用在修改图层的一些属性上
[CATransaction begin];
[CATransaction setAnimationDuration:1];
// 控制动画的速度 (curve)
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
// layer.frame = CGRectMake(200, 100, 50, 50);
// 转换
// layer.transform = CATransform3DMakeRotation(M_PI_2, 0, 0, 1);
layer.transform = CATransform3DRotate(layer.transform, M_PI_2, 1, 1, 1);
layer.transform = CATransform3DScale(layer.transform, 0.9, 0.9, 0.9);
layer.transform = CATransform3DTranslate(layer.transform, 10, 10, 10);
[CATransaction commit];
基础动画
/*
// 基础动画 :控制图层每次改变(大小,位置,旋转)多少
// keyPath 决定了要动画具体做什么事情
// transform.rotation.x 旋转
// transform.scale 刻度(放大缩小)
// transform.translate(position) 移动位置
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
animation.duration = 0.5;
// 从什么状态开始
animation.fromValue = @(0);
// 每次改变多少
animation.byValue = @(M_PI);
// 转到什么状态结束
// animation.toValue = @(20 * M_PI);
// 动画重复多少次
// animation.repeatCount = 100;
animation.repeatDuration = MAXFLOAT;
// 动画做完之后 倒退回来
animation.autoreverses = YES;
[layer addAnimation:animation forKey:@"basic"];
*/
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.duration = 0.2;
animation.fromValue = @(0.8);
animation.toValue = @(1.5);
animation.repeatCount = MAXFLOAT;
animation.autoreverses = YES;
[layer addAnimation:animation forKey:@"scaleAnimation"];
关键帧动画
// 关键帧动画:可以控制图层在不同的时间点的不同的状态
// position 指动画的中心点
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.duration = 0.5;
// 不同的状态
animation.values = @[[NSValue valueWithCGPoint:CGPointMake(30, 80)],[NSValue valueWithCGPoint:CGPointMake(280, 80)],[NSValue valueWithCGPoint:CGPointMake(150, 80)],[NSValue valueWithCGPoint:CGPointMake(200, 80)]];
// 到达不同的位置的时间点 里面是比例
animation.keyTimes = @[@(0),@(0.4),@(0.7),@(1)];
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[layer addAnimation:animation forKey:@"position"];
group动画
- (void)viewDidLoad {
[super viewDidLoad];
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(130, 20, 60, 60)];
imageView.image = [UIImage imageNamed:@"123"];
imageView.layer.cornerRadius = 30;
imageView.clipsToBounds = YES;
[self.view addSubview:imageView];
}
- (void)viewWillAppear:(BOOL)animated
{
// 旋转
CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
basic.duration = 1;
basic.fromValue = @(0);
basic.toValue = @(4 * M_PI);
basic.repeatDuration = 3;
// 往下落
CAKeyframeAnimation *moveKeyframe= [CAKeyframeAnimation animationWithKeyPath:@"position"];
moveKeyframe.values = @[[NSValue valueWithCGPoint:CGPointMake(160, 50)],[NSValue valueWithCGPoint:CGPointMake(160, 550)],[NSValue valueWithCGPoint:CGPointMake(160, 200)],[NSValue valueWithCGPoint:CGPointMake(160, 550)],[NSValue valueWithCGPoint:CGPointMake(160, 350)],[NSValue valueWithCGPoint:CGPointMake(160, 550)]];
moveKeyframe.keyTimes = @[@(0),@(0.2),@(0.4),@(0.6),@(0.8),@(1)];
moveKeyframe.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
// 放大
CAKeyframeAnimation *scaleKeyframe = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
scaleKeyframe.values = @[@(1),@(1.5),@(1.3),@(1.5),@(1.4),@(1.5)];
scaleKeyframe.keyTimes = @[@(0),@(0.2),@(0.4),@(0.6),@(0.8),@(1)];
// 动画组
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[basic,moveKeyframe,scaleKeyframe];
group.duration = 3;
group.removedOnCompletion = NO;
group.fillMode = kCAFillModeForwards;
[imageView.layer addAnimation:group forKey:@"group"];