//
// 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