#import "FirstVC.h"
@implementation FirstVC
/*
    创建xib过程
    1 创建xib(名字和类名相同)
    2 文件拥有者为类名
    3 和类的view连线
 */
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
 
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
//UIView属性动画
- (IBAction)pressPropertyAnimation:(id)sender {
    //1 准备动画
    //参数1: 动画的作用, 用来区分多个动画, 参数二: 传递参数用 nil:OC中使用 NULL:C语言使用
    [UIView beginAnimations:@"改变大小" context:NULL];
    //在准备动画的时候可以设置动画的属性
    [UIView setAnimationDuration:2];//设置动画的持续时间
    [UIView setAnimationDelegate:self];
    [UIView setAnimationWillStartSelector:@selector(startAnimation)];
//    [UIView setAnimationDelay:1];//动画延迟执行时间
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//动画的曲线
    [UIView setAnimationRepeatCount:20];//动画的重复次数
    [UIView setAnimationRepeatAutoreverses:YES];//动画往返执行, 必须设置动画的重复次数
    //2 修改view的属性, 可以同时修改多个属性 注意:不是所有的属性都可以修改的(只有frame, center, bounds, backgroundColor, alpha, transform 可以修改)
    self.changeView.frame = CGRectMake(110100100100);
//    self.changeView.backgroundColor = [UIColor brownColor];
    self.changeView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 255.0 green:arc4random() % 256 255.0 blue:arc4random() % 256 255.0 alpha:0.5];
    //3 提交并执行动画
    [UIView commitAnimations];
}
//UIView过度动画
- (IBAction)pressTranstionAnimation:(id)sender {
    //1 准备动画
    [UIView beginAnimations:@"过度动画" context:NULL];
    [UIView setAnimationDuration:5];
    [UIView setAnimationRepeatCount:50];
    //2 设置过度样式
    //参数1: 过度样式, 参数2: 指定那个View做动画, 参数3: 是否设置缓存
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.changeView cache:YES];
    self.changeView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 255.0 green:arc4random() % 256 255.0 blue:arc4random() % 256 255.0 alpha:0.5];
    //3 提交并执行动画
    [UIView commitAnimations];
}
 
//Block动画
- (IBAction)pressBlockAnimation:(id)sender {
    //只有一行代码 Block动画实质是对UIView动画的封装
    //参数1:动画时长 参数2:Block: 设置要修改的View属性
    /*
    [UIView animateWithDuration:2 animations:^{
        self.changeView.backgroundColor = [UIColor orangeColor];
    }];
     */
    //第二种Block
    /*
    //参数1:动画时长 参数2:Block: 设置要修改的View属性 参数3: 动画完成时调用
    [UIView animateWithDuration:2 animations:^{
        self.changeView.backgroundColor = [UIColor orangeColor];
    } completion:^(BOOL finished) {
        //finished判断动画是否完成
        if (finished) {
            NSLog(@"finished");
        }
    }];
    */
    //第三种Block
    /*
    [UIView animateWithDuration:2 delay:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//        设置要修改的View属性
        self.changeView.backgroundColor = [UIColor orangeColor];
    } completion:^(BOOL finished) {
        //finished判断动画是否完成
        if (finished) {
            NSLog(@"finished");
        }
    }];
     */
    //对过度动画的封装
    //参数1: 改变的View 参数2:动画时长 参数3:动画类型 参数4 Block: 设置要修改的View属性 参数5:完成后的操作
    [UIView transitionWithView:self.changeView duration:2 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
        self.changeView.backgroundColor = [UIColor orangeColor];
    } completion:^(BOOL finished) {
        //finished判断动画是否完成
        if (finished) {
            NSLog(@"finished");
        }
    }];
}
 
#pragma mark - AnimationDelegate
//动画将要开始时调用
- (void)animationWillStart:(NSString *)animationID context:(void *)context
{
    NSLog(@"start: %@, %@", animationID, context);
}
 
//动画结束时调用
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    NSLog(@"stop: %@, %@", animationID, context);
}
 
- (void)startAnimation
{
    NSLog(@"self");
}
 
 
#import "SecondVC.h"
@implementation SecondVC
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSLog(@"%@", NSStringFromCGRect(self.changeView.frame));
    NSLog(@"%f", CGRectGetWidth(self.changeView.frame));
    //UIView和CALayer的关系
    //UIView负责交互, frame以及显示CALayer
    //CALayer负责渲染, 并且它是UIView的一个readonly属性
    /*
    self.changeView.layer.cornerRadius = 100;//设置圆角, 参数是内切圆的半径, 若想画一个圆, 前提是view必须是正方形, 参数应该是view边长的一半
    self.changeView.layer.borderWidth = 1;//设置描边的宽度
    self.changeView.layer.borderColor = [UIColor orangeColor].CGColor;//设置描边的颜色(UIView上的颜色使用的是UIColor, CALayer上使用的颜色是CGColor)
    self.changeView.layer.shadowOffset = CGSizeMake(50, 100);//设置阴影的偏移量 width影响水平偏移(正右负左), height影响垂直偏移(正下负上)
    self.changeView.layer.shadowColor = [UIColor grayColor].CGColor;//阴影的偏移的颜色
    self.changeView.layer.shadowOpacity = 1;//阴影的不透明度, 取值范围(0 ~ 1), 默认是0, 就是透明的
     */
//    CAAnimation抽象类, 使用必须要使用其具体的子类
//    CAPropertyAnimation抽象子类, 需要子类化
//    CABasicAnimation
//    CAKeyframeAnimation
}
 
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
- (void)dealloc {
    [_changeView release];
    [super dealloc];
}
//CABasicAnimation基本动画 没有真正的修改属性值
- (IBAction)pressBasicAnimation:(id)sender {
    //1 创建并指定要修改的属性
//    KeyPath:CAlayer的属性名, 不是所有的属性都可以, 只有在头文件中出现animatable的属性才可以, 可以修改属性的属性, 例如:bounds.size
//    CALayer
    CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"bounds"];
    [basic setDuration:2];
    //2 修改属性值
    basic.fromValue = [NSValue valueWithCGRect:CGRectMake(00, self.changeView.bounds.size.width, self.changeView.bounds.size.height)];
    basic.toValue = [NSValue valueWithCGRect:CGRectMake(00300300)];
//    basic.byValue = 
    //3 添加动画
    //key做区分动画用
    [self.changeView.layer addAnimation:basic forKey:@"changColor"];
}
 
//CAKeyframeAnimation关键帧动画
- (IBAction)pressKeyFrameAnimation:(id)sender {
    /*
    //1 创建动画
    CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"bounds"];
    [keyFrame setDuration:2];
    //2 修改属性
    keyFrame.values = @[[NSValue valueWithCGRect:CGRectMake(0, 0, self.changeView.bounds.size.width, self.changeView.bounds.size.height)], [NSValue valueWithCGRect:CGRectMake(0, 0, 250, 250)], [NSValue valueWithCGRect:CGRectMake(0, 0, 300, 300)]];
//    keyTimes:值代表了出现动画的时刻, 值得范围是0~1, 值必须是递增的, keyTimes和values是一一对应的
    keyFrame.keyTimes = @[@(0.4), @(0.6), @(1)];
    //3 添加动画
    [self.changeView.layer addAnimation:keyFrame forKey:@"keyFrame"];
    */
    CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
    [keyFrame setDuration:10];
    keyFrame.values = @[(id)[UIColor redColor].CGColor, (id)[UIColor orangeColor].CGColor, (id)[UIColor yellowColor].CGColor, (id)[UIColor greenColor].CGColor, (id)[UIColor blueColor].CGColor];
    //keyTimes中的第一个值是0, 不能修改
    keyFrame.keyTimes = @[@(0.3), @(0.5), @(0.6), @(0.7), @(0.9)];
    [self.changeView.layer addAnimation:keyFrame forKey:nil];
}
 
//    CATransaction 过度动画
- (IBAction)pressTransition:(id)sender {
    //1 创建
    CATransition *transition = [CATransition animation];
    [transition setDuration:2];
    //2 设置过度样式
    transition.type = kCATransitionReveal;//控制样式
    transition.subtype = kCATransitionFromTop;//控制方向
    //添加动画
    [self.changeView.layer addAnimation:transition forKey:nil];
}
 
//    CAAnimationGroup 组动画
- (IBAction)pressAnimationGroup:(id)sender {
     
    //1 创建并指定要修改的属性
    //    KeyPath:CAlayer的属性名, 不是所有的属性都可以, 只有在头文件中出现animatable的属性才可以, 可以修改属性的属性, 例如:bounds.size
    //    CALayer
    CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"bounds"];
    [basic setDuration:2];
    //2 修改属性值
    basic.fromValue = [NSValue valueWithCGRect:CGRectMake(00, self.changeView.bounds.size.width, self.changeView.bounds.size.height)];
    basic.toValue = [NSValue valueWithCGRect:CGRectMake(00300300)];
     
    CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
    [keyFrame setDuration:5];
    keyFrame.values = @[(id)[UIColor redColor].CGColor, (id)[UIColor orangeColor].CGColor, (id)[UIColor yellowColor].CGColor, (id)[UIColor greenColor].CGColor, (id)[UIColor blueColor].CGColor];
    //keyTimes中的第一个值是0, 不能修改
    keyFrame.keyTimes = @[@(0.3), @(0.5), @(0.6), @(0.7), @(0.9)];
     
     
     
    // 创建
    //当group动画的时长 > 组中所有动画的最长时长, 动画的时长以组中最长的时长为准
    //当group动画的时长 < 组中所有动画的最长时长, 动画的时长以group的时长为准
    //最合适: group的时长 = 组中所有动画的最长时长
    CAAnimationGroup *group = [CAAnimationGroup animation];
    [group setDuration:10];
    //设置组动画
    group.animations = @[basic, keyFrame];
    //添加动画
    [self.changeView.layer addAnimation:group forKey:nil];
}
@end

ios 关于动画用法的总结的更多相关文章

  1. iOS block-base 动画简单用法+关键帧动画设置线性变化速度的问题

    本文转载至 http://www.tuicool.com/articles/aANBF3m 时间 2014-12-07 20:13:37  segmentfault-博客原文  http://segm ...

  2. iOS核心动画学习整理

    最近利用业余时间终于把iOS核心动画高级技巧(https://zsisme.gitbooks.io/ios-/content/chapter1/the-layer-tree.html)看完,对应其中一 ...

  3. IOS 核心动画之CAKeyframeAnimation - iBaby

    - IOS 核心动画之CAKeyframeAnimation - 简单介绍 是CApropertyAnimation的子类,跟CABasicAnimation的区别是:CABasicAnimation ...

  4. iOS各种动画效果

    ios各种动画效果 最普通动画: //开始动画 [UIView beginAnimations:nil context:nil];  //设定动画持续时间 [UIView setAnimationDu ...

  5. IOS之动画

    IOS之动画   15.1 动画介绍 15.2 Core Animation基础 15.3 隐式动画 15.4 显式动画 15.5 关键帧显式动画 15.6 UIView级别动画 15.1 动画介绍 ...

  6. IOS NSUserDefaults 讲解 用法

    IOS NSUserDefaults 讲解 用法    NSUserDefaults适合存储轻量级的本地数据,比如要保存一个登陆界面的数据,用户名.密码之类的,个人觉得使用NSUserDefaults ...

  7. IOS 动画专题 --iOS核心动画

    iOS开发系列--让你的应用“动”起来 --iOS核心动画 概览 通过核心动画创建基础动画.关键帧动画.动画组.转场动画,如何通过UIView的装饰方法对这些动画操作进行简化等.在今天的文章里您可以看 ...

  8. ios 学习动画的套路 (一)

    你也肯定喜欢炫酷的动画! 在APP中,动画就是一个点睛之笔!可以给用户增加一些独特的体验感,估计也有许多的和我一样的,看着那些觉得不错的动画,也就只能流口水的孩子,毕竟~不知道从哪里下手去写!会连续的 ...

  9. Bodymovin:Bodymovin和Lottie:把AE动画转换成HTML5/Android/iOS原生动画

    转自:https://www.cnblogs.com/zamhown/p/6688369.html 大杀器Bodymovin和Lottie:把AE动画转换成HTML5/Android/iOS原生动画 ...

随机推荐

  1. SPOJ GSS7 Can you answer these queries VII ——树链剖分 线段树

    [题目分析] 问题放到了树上,直接链剖+线段树搞一搞. 调了300行+. (还是码力不够) [代码] #include <cstdio> #include <cstring> ...

  2. Spoj-BIPCSMR16 Team Building

    To make competitive programmers of BUBT, authority decide to take regular programming contest. To ma ...

  3. jsp 详解request对象

    request对象 客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应.它是HttpServletRequest类的实例. 序号 方 法 说 明 1  object ...

  4. POJ3132 Sum of Different Primes

    Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3473   Accepted: 2154 Description A pos ...

  5. hdu 5040 Instrusive【BFS+优先队列】

    11733274 2014-09-26 12:42:31 Accepted 5040 62MS 1592K 4848 B G++ czy 先转一个优先队列的用法: http://www.cppblog ...

  6. AC日记——草地排水 codevs 1993

    1993 草地排水 USACO  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 在农夫约翰的农场上,每 ...

  7. Codeforces Round #512 (Div. 2, based on Technocup 2019 Elimination Round 1) E. Vasya and Good Sequences

    题目链接 官网题解写的好清楚,和昨晚Aguin说的一模一样…… 这题只和每个数1的个数有关,设每个数1的个数的数组为$b$,就是首先一段如果是好的,要满足两个条件: 1.这一段$b$数组和为偶数,因为 ...

  8. Wormholes(spfa判负环)

      POJ - 3259—— Wormholes Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & % ...

  9. @font-face制作小图标的实践

    1.为啥要用font-face制作小图标 1)适用性:一个图标字体要比一系列的图像要小,一旦字体图标加载完,图标则会立刻显示出来,不需要去下载一个图像. 2)可扩展性:可以使用font-size对图标 ...

  10. Servlet(生命周期)

    <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" ...