// 设置阴影透明度
_redView.layer.shadowOpacity = 1;
// 设置阴影颜色
_redView.layer.shadowColor = [UIColor yellowColor].CGColor;
// 设置阴影圆角半径
_redView.layer.shadowRadius = 10;
// 设置圆角半径
_redView.layer.cornerRadius = 50;
// 设置边框半径
_redView.layer.borderColor = [UIColor whiteColor].CGColor;
// 设置边框半径
_redView.layer.borderWidth = 2;
// 超出layer边框的全部裁剪掉
_imageView.layer.masksToBounds = YES;
//旋转 x y z
_imageView.layer.transform = CATransform3DMakeRotation(M_PI, 1, 1, 0);
//平移
_imageView.layer.transform = CATransform3DMakeTranslation(200, 200, 0);
//缩放
_imageView.layer.transform = CATransform3DMakeScale(1, 0.5, 1);
// 利用KVC改变形变
NSValue *rotation = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1, 1, 0)];
[_imageView.layer setValue:rotation forKeyPath:@"transform"];
[_imageView.layer setValue:@M_PI forKeyPath:@"transform.rotation"];
[_imageView.layer setValue:@0.5 forKeyPath:@"transform.scale"];
// 平移x轴
[_imageView.layer setValue:@200 forKeyPath:@"transform.translation.x"];
// 创建一个图层
CALayer *layer = [CALayer layer];
// 设置尺寸
layer.bounds = CGRectMake(0, 0, 100, 100);
// 设置位置
layer.position = CGPointMake(100, 100);
// 设置颜色
layer.backgroundColor = [UIColor redColor].CGColor;
// 设置内容
layer.contents = (__bridge id)[UIImage imageNamed:@"阿狸头像"].CGImage;
[self.view.layer addSublayer:layer];
// 创建动画对象
CABasicAnimation *anim = [CABasicAnimation animation];
// 设置动画的属性
anim.keyPath = @"transform.scale";
// 设置属性改变的值
anim.toValue = @0.5;
// 设置动画时长
anim.duration = 0.25;
// 取消反弹
// 动画执行完毕之后不要把动画移除
anim.removedOnCompletion = NO;
// 保持最新的位置
anim.fillMode = kCAFillModeForwards;
// 重复动画的次数
anim.repeatCount = MAXFLOAT;
// 给图层添加了动画
[_layer addAnimation:anim forKey:nil];
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
// 设置动画属性
anim.keyPath = @"position";
NSValue *v1 = [NSValue valueWithCGPoint:CGPointZero];
NSValue *v2 = [NSValue valueWithCGPoint:CGPointMake(160, 160)];
NSValue *v3 = [NSValue valueWithCGPoint:CGPointMake(270, 0)];
anim.values = @[v1,v2,v3];
anim.duration = 2;
[_redView.layer addAnimation:anim forKey:nil];
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"transform.rotation";
anim.values = @[@(angle2radian(-5)),@(angle2radian(5)),@(angle2radian(-5))];
anim.repeatCount = MAXFLOAT;
anim.duration = 0.5;
[_imageView.layer addAnimation:anim forKey:nil];
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
// 设置动画属性
anim.keyPath = @"position";
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 200)];
anim.path = path.CGPath;
anim.duration = 0.25
// 取消反弹
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards
anim.repeatCount = MAXFLOAT;
[_redView.layer addAnimation:anim forKey:nil];
// 转场动画
CATransition *transition = [CATransition animation];
transition.type = @"rippleEffect";
transition.duration = 0.8;
transition.subtype = kCATransitionFromLeft;
[self.vc.navigationController.view.layer addAnimation:transition forKey:nil];
CATransition中type的值:
1 fade = 1, //淡入淡出
2 push, //推挤
3 reveal, //揭开
4 moveIn, //覆盖
5 cube, //立方体
6 suckEffect, //吮吸
7 oglFlip, //翻转
8 rippleEffect, //波纹
9 pageCurl, //翻页
10 pageUnCurl, //反翻页
11 cameraIrisHollowOpen, //开镜头
12 cameraIrisHollowClose, //关镜头
下面这个几个用在UIView的setAnimationTransition方法中
13 UIViewAnimationTransitionCurlDown, //下翻页
14 UIViewAnimationTransitionCurlUp, //上翻页
15 UIViewAnimationTransitionFlipFromLeft, //左翻转
16 UIViewAnimationTransitionFlipFromRight, //右翻转
// 动画组
CABasicAnimation *rotation = [CABasicAnimation animation];
rotation.keyPath = @"transform.rotation";
rotation.toValue = @M_PI_2;
CABasicAnimation *position = [CABasicAnimation animation];
position.keyPath = @"position";
position.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 250)];
CABasicAnimation *scale = [CABasicAnimation animation];
scale.keyPath = @"transform.scale";
scale.toValue = @0.5;
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[rotation,position,scale];
group.duration = 2;
// 取消反弹
group.removedOnCompletion = NO;
group.fillMode = kCAFillModeForwards;
[_redView.layer addAnimation:group forKey:nil];
防止动画结束后回到初始状态只需设置removedOnCompletion、fillMode两个属性就可以了
transformAnima.removedOnCompletion = NO;
transformAnima.fillMode = kCAFillModeForwards;
//
动画速度变化
transformAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
kCAMediaTimingFunctionLinear 传这个值,在整个动画时间内动画都是以一个相同的速度来改变。也就是匀速运动。
kCAMediaTimingFunctionEaseIn 使用该值,动画开始时会较慢,之后动画会加速。
kCAMediaTimingFunctionEaseOut 使用该值,动画在开始时会较快,之后动画速度减慢。
kCAMediaTimingFunctionEaseInEaseOut 使用该值,动画在开始和结束时速度较慢,中间时间段内速度较快。