#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(110, 100, 100, 100);
//    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(0, 0, self.changeView.bounds.size.width, self.changeView.bounds.size.height)];
    basic.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 300, 300)];
//    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(0, 0, self.changeView.bounds.size.width, self.changeView.bounds.size.height)];
    basic.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 300, 300)];
     
    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. BZOJ3532 [Sdoi2014]Lis 【网络流退流】

    题目 给定序列A,序列中的每一项Ai有删除代价Bi和附加属性Ci.请删除若 干项,使得4的最长上升子序列长度减少至少1,且付出的代价之和最小,并输出方案. 如果有多种方案,请输出将删去项的附加属性排序 ...

  2. html的常见meta标签信息

    设置页面不缓存<meta http-equiv="pragma" content="no-cache"><meta http-equiv=&q ...

  3. jquery.fullPage.js全屏滚动插件

    注:本文内容复制于http://www.51xuediannao.com/js/jquery/jquery.fullPage.html 和 http://www.360doc.com/content/ ...

  4. 洛谷——P2298 Mzc和男家丁的游戏

    P2298 Mzc和男家丁的游戏 题目背景 mzc与djn的第二弹. 题目描述 mzc家很有钱(开玩笑),他家有n个男家丁(做过上一弹的都知道).他把她们召集在了一起,他们决定玩捉迷藏.现在mzc要来 ...

  5. 《Java虚拟机原理图解》1.5、 class文件中的方法表集合--method方法在class文件中是怎样组织的

    0. 前言 了解JVM虚拟机原理是每一个Java程序员修炼的必经之路.但是由于JVM虚拟机中有很多的东西讲述的比较宽泛,在当前接触到的关于JVM虚拟机原理的教程或者博客中,绝大部分都是充斥的文字性的描 ...

  6. 【Gradle】配置中引用的jar包版本后面自动加冒号导致引入jar包失败的问题/gradle中引用jar包版本不一致的问题/gradle中引用jar失败的问题 解决方法

    idea中 gradle中 引用jar包,版本后面默认加:的问题 gradle中引用jar包版本不一致的问题 gradle中引用jar失败的问题 如上题目所示,三个问题其实都是同一样的简单又恶心,因为 ...

  7. 基于MNIST数据的softmax regression

    跟着tensorflow上mnist基本机器学习教程联系 首先了解sklearn接口: sklearn.linear_model.LogisticRegression In the multiclas ...

  8. BUPT复试专题—寻找变化前01序列(2016)

    题目描述 给你一个01序列,HDLC协议处理的话,如果出现连续的5个1会补1个0.例如1111110,会变成11111010. 现在给你一个经过HDLC处理后的01序列,你需要找到HDLC处理之前的0 ...

  9. [转]Go基础之锁的初识

    当我们的程序就一个线程的时候是不需要用到锁的,但是通常我们实际的代码不会是单个线程的,所有这个时候就需要用到锁了,那么关于锁的使用场景主要涉及到哪些呢? 当我们多个线程在读相同的数据的时候则是需要加锁 ...

  10. Swift的可选链,类型转换和扩展

    可选链(Optional Chaining) 可选链是一种请求或调用属性.方法,子脚本的过程. 可选性体现于请求或调用的目标当前可能为nil.若不为nil则成功调用.否则返回nil并将链失效. 调用可 ...