//
// ViewController.m
// CALayerAnimations
//
// Created by Augus on 16/2/17.
// Copyright © 2016年 com.iBokanWisdom. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
{
int _index;
}
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
@property (weak, nonatomic) IBOutlet UIView *testView;
@property (strong, nonatomic) CALayer * cclayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self creatLayer];
_index = 0;
}
//1、layer常用属性
- (void)layerProperty
{
//设置view圆角,当view是imageView时需要注意
// self.testView.layer.cornerRadius = 50;
// self.imageV.layer.cornerRadius = 50;
self.imageV.layer.masksToBounds = YES;
// self.imageV.clipsToBounds = YES;
//阴影的设置
self.testView.layer.shadowOpacity = 1;//透明度
self.testView.layer.shadowColor = [[UIColor redColor]CGColor];//阴影颜色
self.testView.layer.shadowOffset = CGSizeMake(60, 10);//阴影的偏移
self.testView.layer.shadowRadius = 20;//阴影圆角
self.testView.layer.borderWidth = 10;//设置边框
self.testView.layer.borderColor = [[UIColor blueColor]CGColor];//设置边框颜色
}
//2、layer3D变换
- (void)layerTransform3D
{
[UIView animateWithDuration:1 animations:^{
// CATransform3D t = CATransform3DMakeTranslation(-100, 0, 0);
// CATransform3D t = CATransform3DTranslate(self.testView.layer.transform, -100, 0, 0);//多次移动的方式
CATransform3D t = CATransform3DRotate(self.imageV.layer.transform, M_PI_2, 1, 0, 0);
// CATransform3D t = CATransform3DScale(self.imageV.layer.transform, 0.5, 2, 1);
//
// CATransform3D t = CATransform3DConcat(<#CATransform3D a#>, <#CATransform3D b#>);
self.imageV.layer.transform = t;
}];
}
//3、手动创建layer
- (void)creatLayer
{
self.cclayer = [CALayer layer];
self.cclayer.backgroundColor = [UIColor cyanColor].CGColor;
self.cclayer.bounds = CGRectMake(0, 0, 100, 100);
//锚点(就是给layer的参照点, (0.0) 点表示左上点 (0.5,0.5)表示中心点)
self.cclayer.anchorPoint = CGPointMake(0, 0);
//position(这个位置移动的是坐标原点)
//self.cclayer.position = CGPointMake(100, 100);
[self.view.layer addSublayer:self.cclayer];
}
//4、隐式动画
- (void)layerHiddenAnimation
{
//layer隐式动画,只有非rootLayer才有隐式动画
[CATransaction begin];
[CATransaction setDisableActions:YES];//这个默认的就是yes 要使用动画的时候不用写出来
[CATransaction setAnimationDuration:2];
self.cclayer.position = CGPointMake(100, 100);
[CATransaction commit];//提交动画
}
//5、layerBaseAnimation
- (void)layerBaseAnimation
{
CABasicAnimation * baseAnimation = [CABasicAnimation animation];
// baseAnimation.keyPath = @"position";
// baseAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
baseAnimation.keyPath = @"transform.rotation";//使用系统的字符串来指定动画的类型
baseAnimation.toValue = @(M_PI_2);
baseAnimation.duration = 1;
baseAnimation.removedOnCompletion = NO;//动画结束之后是否移除动画后的场景
baseAnimation.fillMode = kCAFillModeForwards;//当前动画场景的填充效果(这和上一句是配合着用的)
//以上只是创建了一个layerBaseAnimation动画
[self.imageV.layer addAnimation:baseAnimation forKey:nil];//为imageV添加layerBaseAnimation
}
//6、关键帧动画
- (void)layerKeyFrameAnimation
{
// CAKeyframeAnimation * keyFrameAnim = [CAKeyframeAnimation animation];
// keyFrameAnim.keyPath = @"position";
// keyFrameAnim.duration = 3;
// NSValue * value0 = [NSValue valueWithCGPoint:CGPointMake(0, 0)];//因为默认的第一帧是没有动画效果的
// NSValue * value1 = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
// NSValue * value2 = [NSValue valueWithCGPoint:CGPointMake(200, 100)];
// NSValue * value3 = [NSValue valueWithCGPoint:CGPointMake(200, 200)];
//
// keyFrameAnim.values = @[value0,value1,value2,value3];
//
// keyFrameAnim.removedOnCompletion = NO;
// keyFrameAnim.fillMode = kCAFillModeForwards;
// [self.cclayer addAnimation:keyFrameAnim forKey:nil];
//用贝塞尔曲线画一个路径
UIBezierPath * bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(30, 100, 300, 200)];
CAKeyframeAnimation * keyAnim = [CAKeyframeAnimation animation];
keyAnim.keyPath = @"position";
keyAnim.path = bezierPath.CGPath;
keyAnim.duration = 2;
keyAnim.removedOnCompletion = NO;
keyAnim.fillMode = kCAFillModeForwards;
[self.cclayer addAnimation:keyAnim forKey:nil];
}
//7、转场动画
- (void)transitionAnimaion
{
//第一
_index ++;
if (_index == 5) {
_index = 1;
}
NSString * name = [NSString stringWithFormat:@"%d.png",_index];
self.imageV.image = [UIImage imageNamed:name];
//第二
CATransition * transition = [CATransition animation];
//第三
transition.duration = 1;
transition.type = @"cube";//可以用的类型如下:
transition.subtype = kCATransitionFromRight;
//第四
transition.removedOnCompletion = NO;
transition.fillMode = kCAFillModeForwards;
//第五
[self.imageV.layer addAnimation:transition forKey:nil];
/*常用的转场效果(首字母小写)
typedef enum : NSUInteger {
Fade = 1, //淡入淡出
Push, //推挤
Reveal, //揭开
MoveIn, //覆盖
Cube, //立方体
SuckEffect, //吮吸
OglFlip, //翻转
RippleEffect, //波纹
PageCurl, //翻页
PageUnCurl, //反翻页
CameraIrisHollowOpen, //开镜头
CameraIrisHollowClose, //关镜头
CurlDown, //下翻页
CurlUp, //上翻页
FlipFromLeft, //左翻转
FlipFromRight, //右翻转
} AnimationType;
*/
}
//8、动画组
- (void)layerAnimationGroup
{
CABasicAnimation * baseOne = [CABasicAnimation animation];
baseOne.keyPath = @"position";
baseOne.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
CABasicAnimation * baseTwo = [CABasicAnimation animation];
baseTwo.keyPath = @"transform.rotation";
baseTwo.toValue = @(M_PI_2);
CAAnimationGroup * group = [CAAnimationGroup animation];
group.duration = 2;
group.animations = @[baseOne,baseTwo];
group.removedOnCompletion = NO;
group.fillMode = kCAFillModeForwards;
[self.cclayer addAnimation:group forKey:nil];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// [self layerProperty];
// [self layerTransform3D];
// [self creatLayer];
// [self layerHiddenAnimation];
// [self layerBaseAnimation];
[self layerKeyFrameAnimation];
// [self transitionAnimaion];
// [self layerAnimationGroup];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end