CALayerAnimations

来源:互联网 发布:ps软件快捷键大全 编辑:程序博客网 时间:2024/05/21 09:00

//

//  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) IBOutletUIImageView *imageV;

@property (weak,nonatomic) IBOutletUIView *testView;

@property (strong,nonatomic) CALayer * cclayer;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    [selfcreatLayer];

    

    _index =0;

}


//1layer常用属性

- (void)layerProperty

{

    //设置view圆角,当viewimageView时需要注意

//    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];//设置边框颜色

}


//2layer3D变换

- (void)layerTransform3D

{

    [UIViewanimateWithDuration: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 = [CALayerlayer];

    self.cclayer.backgroundColor = [UIColorcyanColor].CGColor;

    self.cclayer.bounds =CGRectMake(0,0, 100,100);

    //锚点(就是给layer的参照点,0.0点表示左上点 0.50.5)表示中心点)

    self.cclayer.anchorPoint =CGPointMake(0,0);

    //position(这个位置移动的是坐标原点)

    //self.cclayer.position = CGPointMake(100, 100);

    [self.view.layeraddSublayer:self.cclayer];

}


//4、隐式动画

- (void)layerHiddenAnimation

{

    //layer隐式动画,只有非rootLayer才有隐式动画

    [CATransactionbegin];

    [CATransactionsetDisableActions:YES];//这个默认的就是yes要使用动画的时候不用写出来

    [CATransactionsetAnimationDuration:2];

    self.cclayer.position =CGPointMake(100,100);

    [CATransactioncommit];//提交动画

    

}


//5layerBaseAnimation

- (void)layerBaseAnimation

{

    CABasicAnimation * baseAnimation = [CABasicAnimationanimation];

//    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.layeraddAnimation: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 = [UIBezierPathbezierPathWithOvalInRect: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.cclayeraddAnimation:keyAnim forKey:nil];

}


//7、转场动画

- (void)transitionAnimaion

{

    //第一

    _index ++;

    if (_index ==5) {

        _index =1;

    }

    NSString * name = [NSStringstringWithFormat:@"%d.png",_index];

    self.imageV.image = [UIImageimageNamed:name];

    //第二

    CATransition * transition = [CATransitionanimation];

    //第三

    transition.duration =1;

    transition.type =@"cube";//可以用的类型如下:

    transition.subtype =kCATransitionFromRight;

    //第四

    transition.removedOnCompletion =NO;

    transition.fillMode =kCAFillModeForwards;

    //第五

    [self.imageV.layeraddAnimation: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 = [CABasicAnimationanimation];

    baseOne.keyPath =@"position";

    baseOne.toValue = [NSValuevalueWithCGPoint:CGPointMake(100,100)];

    

    CABasicAnimation * baseTwo = [CABasicAnimationanimation];

    baseTwo.keyPath =@"transform.rotation";

    baseTwo.toValue =@(M_PI_2);

    

    CAAnimationGroup * group = [CAAnimationGroupanimation];

    group.duration =2;

    group.animations =@[baseOne,baseTwo];

    group.removedOnCompletion =NO;

    group.fillMode =kCAFillModeForwards;

    [self.cclayeraddAnimation:group forKey:nil];

}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

//    [self layerProperty];

    

//    [self layerTransform3D];

    

//    [self creatLayer];

    

//    [self layerHiddenAnimation];

    

//    [self layerBaseAnimation];

    

    [selflayerKeyFrameAnimation];

    

//    [self transitionAnimaion];

    

//    [self layerAnimationGroup];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

0 0
原创粉丝点击