代码改变世界

CALayer()CoreAnimation 核心动画 简称 CA

2015-10-16 05:20  真实16  阅读(182)  评论(0编辑  收藏  举报

/*

 CoreAnimation 核心动画  简称 CA

 

 一、动画块

 frame bounds center alpha

 Transition 过渡

 transform 动画效果

 

 二、之前使用过得UIView动画,其本质上也是CoreAnimation实现的,知识对他里面的动画进行了封装

 

 视图(UIView)支持动画的属性frame、bounds、center、alpha、transform、以及动画延迟、动画曲线(淡入淡出、动画过渡)、重复次数

 

 +(void)setAnimationDelegate:(id)delegate;

 +(void)setAnimationWillStartSelector:(SEL)selector; 当动画结束的时候,执行delegate对象的selector,并且把beginAnimations:context:中传入的参数传进selector

 + (void)setAnimationDidStopSelector:(SEL)selector;  当动画结束时,执行delegate对象的selector,并且把beginAnimations:context:中传入的参数传进selector

 

 + (void)setAnimationDuration:(NSTimeInterval)duration;   动画的持续时间,秒为单位

 

 + (void)setAnimationDelay:(NSTimeInterval)delay;  动画延迟delay秒后再开始

 

 + (void)setAnimationStartDate:(NSDate *)startDate;   动画的开始时间,默认为now

 

 + (void)setAnimationCurve:(UIViewAnimationCurve)curve;  动画的节奏控制(过渡)

 

 + (void)setAnimationRepeatCount:(float)repeatCount;  动画的重复次数

 

 + (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses;  如果设置为YES,代表动画每次重复执行的效果会跟上一次相反

 

 + (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache;  设置视图view的过渡效果, transition指定过渡类型, cache设置YES代表使用视图缓存,性能较好

 */

#import "ViewController.h"

 

@interface ViewController ()

{

    UIImageView *imageView;

}

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

 

    imageView = [[UIImageView alloc]initWithFrame:self.view.frame];

    imageView.image = [UIImage imageNamed:@"7.jpg"];

//    imageView.contentMode = UIViewContentModeScaleAspectFit;

    [self.view addSubview:imageView];

    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(0, 0, 100, 40);

    button.center = self.view.center;

    button.backgroundColor = [UIColor brownColor];

    [button addTarget:self action:@selector(viewAnimation2) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

 

 

}

 

#pragma mark -- 1、frame bounds center alpha ---

- (void)viewAnimation1 {

#pragma mark - 方法1

//    在一段时间内 执行完命令

//    [UIView animateWithDuration:3 animations:^{

//        imageView.alpha = 0.5;

//    }];

    

#pragma mark - 方法2

//  开始动画

    [UIView beginAnimations:@"animation" context:nil];

    

//    这只动画的持续时间

    [UIView setAnimationDuration:3];

    

//    ..... 动画效果

    imageView.alpha = 0.5;

    imageView.bounds = CGRectMake(0, 0, 100, 100);

    imageView.center = CGPointMake(50, 200);

//    imageView.frame = CGRectMake(100, 100, 100, 100);

    

//    提交动画  会去执行动画

    [UIView commitAnimations];

}

 

#pragma mark - 2、Transition

/*

 typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {

 UIViewAnimationTransitionNone,

 UIViewAnimationTransitionFlipFromLeft, 从左面翻转

 UIViewAnimationTransitionFlipFromRight,从右面翻转

 UIViewAnimationTransitionCurlUp, 向上翻页

 UIViewAnimationTransitionCurlDown,向下翻页

 };

 */

- (void)viewAnimation2 {

//    开始动画

    [UIView beginAnimations:nil context:nil];

//    设置动画持续时间

//    [UIView setAnimationDuration:3];

//    设置 UIView 的过渡动画

    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:imageView cache:YES];

    

#pragma mark - 3、UIViewAnimationCurve

    /*

     typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {

     UIViewAnimationCurveEaseInOut,         // slow at beginning and end  慢进慢出

     UIViewAnimationCurveEaseIn,            // slow at beginning  快进

     UIViewAnimationCurveEaseOut,           // slow at end  快出

     UIViewAnimationCurveLinear   匀速

     };

     */

    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

//    设置 代理(检测动画结束)

    [UIView setAnimationDelegate:self];

    [UIView setAnimationDidStopSelector:@selector(finishAnimation)];

//    提交动画

    [UIView commitAnimations];

    

}

 

//  动画结束之后触发的方法

- (void)finishAnimation {

    [UIView beginAnimations:@"o" context:nil];

    [UIView setAnimationDuration:3];

    imageView.alpha = 0.1;

    imageView.bounds = CGRectZero;

    imageView.center = CGPointMake(100, 100);

    

    [UIView commitAnimations];

}

 

#pragma mark - 4、transform

/*

 imageView.transform=CGAffineTransformScale(imageView.transform, 0.5, 0.5); // 实现的是放大和缩小imageView.transform=CGAffineTransformRotate(imageView.transform, M_PI_4); //实现的是旋转 imageView.transform=CGAffineTransformTranslate(imageView.transform, 20, 0); //实现的是平移

 */

- (void)viewAnimation3 {

    

    [UIView beginAnimations:@"animation" context:nil];

    [UIView setAnimationDuration:3];

//    transform 如果没有还原transform 他会保持 改变后的模样

    imageView.transform = CGAffineTransformScale(imageView.transform, 0.5, 0.5);

    

    [UIView setAnimationDelegate:self];

    [UIView setAnimationDidStopSelector:@selector(restore)];

    [UIView commitAnimations];

    

}

 

- (void)restore {

    [UIView animateWithDuration:3 animations:^{

        imageView.transform = CGAffineTransformIdentity;

 

    }];

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end