iOS 动画 第七章 隐式动画

    [self changeColorAnimationTest];
    
    //图层行为
    //[self changeColorAnimationTest2];
    
    //ActionForKey
    //[self uiviewActionForKeyTest];
    
    //推进过渡
    //[self changeColorAnimationTest3];
    
    //呈现图层
    //[self hitTestTest];

事务

//实际上动画执行的时间取决于当前事务的设置,动画类型取决于图层行为。
//事务实际上是Core Animation用来包含一系列属性动画集合的机制,任何用指定事务去改变可以做动画的图层属性都不会立刻发生变化,而是当事务一旦提交的时候开始用一个动画过渡到新值。
//事务是通过CATransaction类来做管理;CATransaction没有属性或者实例方法,并且也不能用+alloc和-init方法创建它。但是可以用+begin和+commit分别来入栈或者出栈。
//你可以通过+setAnimationDuration:方法设置当前事务的动画时间,或者通过+animationDuration方法来获取值(默认0.25秒)。
//Core Animation在每个run loop周期中自动开始一次新的事务(run loop是iOS负责收集用户输入,处理定时器或者网络事件并且重新绘制屏幕的东西),即使你不显式的用[CATransaction begin]开始一次事务,任何在一次run loop循环中属性的改变都会被集中起来,然后做一次0.25秒的动画。
//UIView有两个方法,+beginAnimations:context:和+commitAnimations,和CATransaction的+begin和+commit方法类似。实际上在+beginAnimations:context:和+commitAnimations之间所有视图或者图层属性的改变而做的动画都是由于设置了CATransaction的原因。
//在iOS4中,苹果对UIView添加了一种基于block的动画方法:+animateWithDuration:animations:。这样写对做一堆的属性动画在语法上会更加简单,但实质上它们都是在做同样的事情。

- (void)changeColorAnimationTest {
    layerView = [[UIView alloc] init];
    layerView.frame = CGRectMake(20.0, 20.0, 300, 300);
    layerView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:layerView];
    
    UIButton *changeColorBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    changeColorBtn.frame = CGRectMake(50, 200, 200, 44.0);
    [changeColorBtn setTitle:@"change Color" forState:(UIControlStateNormal)];
    [changeColorBtn addTarget:self action:@selector(changeColorAction) forControlEvents:UIControlEventTouchUpInside];
    changeColorBtn.backgroundColor = [UIColor redColor];
    [layerView addSubview:changeColorBtn];
    
    colorLayer = [CALayer layer];
    colorLayer.frame = CGRectMake(50.0f, 50.0f, 100.0f, 100.0f);
    colorLayer.backgroundColor = [UIColor blueColor].CGColor;
    //add it to our view
    [layerView.layer addSublayer:colorLayer];
}

- (void)changeColorAction {
    //begin a new transaction
    [CATransaction begin];
    //set the animation duration to 1 second
    [CATransaction setAnimationDuration:1.0f];
    
    //完成块
    //add the spin animation on completion
    [CATransaction setCompletionBlock:^{
        //rotate the layer 90 degrees
        CGAffineTransform transform = colorLayer.affineTransform;
        transform = CGAffineTransformRotate(transform, M_PI_2);
        colorLayer.affineTransform = transform;
    }];
    
    //randomize the layer background color
    CGFloat red = arc4random() / (CGFloat)INT_MAX;
    CGFloat green = arc4random() / (CGFloat)INT_MAX;
    CGFloat blue = arc4random() / (CGFloat)INT_MAX;
    colorLayer.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor;
    
    //commit the transaction
    [CATransaction commit];
}

完成块

//基于UIView的block的动画允许你在动画结束的时候提供一个完成的动作。CATranscation接口提供的+setCompletionBlock:

图层行为

- (void)changeColorAnimationTest2 {
    layerView = [[UIView alloc] init];
    layerView.frame = CGRectMake(20.0, 20.0, 300, 300);
//    layerView.backgroundColor = [UIColor blackColor];
    layerView.layer.backgroundColor = [UIColor blueColor].CGColor;
    [self.view addSubview:layerView];
    
    UIButton *changeColorBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    changeColorBtn.frame = CGRectMake(50, 200, 200, 44.0);
    [changeColorBtn setTitle:@"change Color" forState:(UIControlStateNormal)];
    [changeColorBtn addTarget:self action:@selector(changeColorAction2) forControlEvents:UIControlEventTouchUpInside];
    changeColorBtn.backgroundColor = [UIColor redColor];
    [layerView addSubview:changeColorBtn];
    
//    colorLayer = [CALayer layer];
//    colorLayer.frame = CGRectMake(50.0f, 50.0f, 100.0f, 100.0f);
//    colorLayer.backgroundColor = [UIColor blueColor].CGColor;
//    //add it to our view
//    [layerView.layer addSublayer:colorLayer];
}

//UIView动画没有过渡 当CALayer的属性被修改时候,它会调用-actionForKey:方法,传递属性的名称。
//图层首先检测它是否有委托,并且是否实现CALayerDelegate协议指定的-actionForLayer:forKey方法。如果有,直接调用并返回结果。
//如果没有委托,或者委托没有实现-actionForLayer:forKey方法,图层接着检查包含属性名称对应行为映射的actions字典。
//如果actions字典没有包含对应的属性,那么图层接着在它的style字典接着搜索属性名。
//最后,如果在style里面也找不到对应的行为,那么图层将会直接调用定义了每个属性的标准行为的-defaultActionForKey:方法。
// //[CATransaction setDisableActions:YES];//可以用来对所有属性打开或者关闭隐式动画。

//UIView关联的图层禁用了隐式动画,对这种图层做动画的唯一办法就是使用UIView的动画函数(而不是依赖CATransaction),或者继承UIView,并覆盖-actionForLayer:forKey:方法,或者直接创建一个显式动画
//对于单独存在的图层,我们可以通过实现图层的-actionForLayer:forKey:委托方法,或者提供一个actions字典来控制隐式动画。

- (void)changeColorAction2 {
    //begin a new transaction
    [CATransaction begin];
    //set the animation duration to 1 second
    [CATransaction setAnimationDuration:1.0f];
    
    //完成块
    //add the spin animation on completion
    [CATransaction setCompletionBlock:^{
        //rotate the layer 90 degrees
        CGAffineTransform transform = colorLayer.affineTransform;
        transform = CGAffineTransformRotate(transform, M_PI_2);
        colorLayer.affineTransform = transform;
    }];
    
    //randomize the layer background color
    CGFloat red = arc4random() / (CGFloat)INT_MAX;
    CGFloat green = arc4random() / (CGFloat)INT_MAX;
    CGFloat blue = arc4random() / (CGFloat)INT_MAX;
    layerView.layer.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor;
    
    //commit the transaction
    [CATransaction commit];
}

- (void)uiviewActionForKeyTest {
    layerView = [[UIView alloc] init];
    layerView.frame = CGRectMake(20.0, 20.0, 300, 300);
    //    layerView.backgroundColor = [UIColor blackColor];
    layerView.layer.backgroundColor = [UIColor blueColor].CGColor;
    [self.view addSubview:layerView];
    
    
    //test layer action when outside of animation block   return nil no animation
    NSLog(@"Outside: %@", [layerView actionForLayer:layerView.layer forKey:@"backgroundColor"]);
    //begin animation block
    [UIView beginAnimations:nil context:nil];
    //test layer action when inside of animation block
    NSLog(@"Inside: %@", [layerView actionForLayer:layerView.layer forKey:@"backgroundColor"]);
    //end animation block
    [UIView commitAnimations];
}

//行为通常是一个被Core Animation隐式调用的显式动画对象。
- (void)changeColorAnimationTest3 {
    layerView = [[UIView alloc] init];
    layerView.frame = CGRectMake(20.0, 20.0, 300, 300);
    layerView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:layerView];
    
    UIButton *changeColorBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    changeColorBtn.frame = CGRectMake(50, 200, 200, 44.0);
    [changeColorBtn setTitle:@"change Color" forState:(UIControlStateNormal)];
    [changeColorBtn addTarget:self action:@selector(changeColorAction3) forControlEvents:UIControlEventTouchUpInside];
    changeColorBtn.backgroundColor = [UIColor redColor];
    [layerView addSubview:changeColorBtn];
    
    colorLayer = [CALayer layer];
    colorLayer.frame = CGRectMake(50.0f, 50.0f, 100.0f, 100.0f);
    colorLayer.backgroundColor = [UIColor blueColor].CGColor;
    //add a custom action
    CATransition *transition = [CATransition animation];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromLeft;
    colorLayer.actions = @{@"backgroundColor": transition};
    
    //add it to our view
    [layerView.layer addSublayer:colorLayer];
}

- (void)changeColorAction3 {
    //randomize the layer background color
    CGFloat red = arc4random() / (CGFloat)INT_MAX;
    CGFloat green = arc4random() / (CGFloat)INT_MAX;
    CGFloat blue = arc4random() / (CGFloat)INT_MAX;
    colorLayer.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor;
}

呈现与模型

//当设置CALayer的属性,实际上是在定义当前事务结束之后图层如何显示的模型.Core Animation扮演了一个控制器的角色,并且负责根据图层行为和事务设置去不断更新视图的这些属性在屏幕上的状态
//在iOS中,屏幕每秒钟重绘60次。如果动画时长比60分之一秒要长,Core Animation就需要在设置一次新值和新值生效之间,对屏幕上的图层进行重新组织。这意味着CALayer除了“真实”值(就是你设置的值)之外,必须要知道当前显示在屏幕上的属性值的记录。
//每个图层属性的显示值都被存储在一个叫做呈现图层的独立图层当中,他可以通过-presentationLayer方法来访问。
//呈现树通过图层树中所有图层的呈现图层所形成。注意呈现图层仅仅当图层首次被提交(就是首次第一次在屏幕上显示)的时候创建,所以在那之前调用-presentationLayer将会返回nil。
//在呈现图层上调用–modelLayer将会返回它正在呈现所依赖的CALayer。通常在一个图层上调用-modelLayer会返回–self

//个是同步动画,一个是处理用户交互。

//如果你在实现一个基于定时器的动画(见第11章“基于定时器的动画”),而不仅仅是基于事务的动画,这个时候准确地知道在某一时刻图层显示在什么位置就会对正确摆放图层很有用了。
//如果你想让你做动画的图层响应用户输入,你可以使用-hitTest:方法(见第三章“图层几何学”)来判断指定图层是否被触摸,这时候对呈现图层而不是模型图层调用-hitTest:会显得更有意义,因为呈现图层代表了用户当前看到的图层位置,而不是当前动画结束之后的位置。

- (void)hitTestTest {
    //create a red layer
    colorLayer = [CALayer layer];
    colorLayer.frame = CGRectMake(0, 0, 100, 100);
    colorLayer.position = CGPointMake(self.view.bounds.size.width/2.0, self.view.bounds.size.height/2.0f);
    colorLayer.backgroundColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:colorLayer];
}
/*
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //get the touch point
    CGPoint point = [[touches anyObject] locationInView:self.view];
    //check if  we have tapped the moving layer
    if ([colorLayer.presentationLayer hitTest:point]) {
        //randomize the layer background color
        CGFloat red = arc4random() / (CGFloat)INT_MAX;
        CGFloat green = arc4random() / (CGFloat)INT_MAX;
        CGFloat blue = arc4random() / (CGFloat)INT_MAX;
        colorLayer.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor;
    } else {
        //otherwise (slowly)   move the layer to new position
        [CATransaction begin];
        [CATransaction setAnimationDuration:4.0];
        colorLayer.position = point;
        [CATransaction commit];
    }
}
*/
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本章开始将正式进入动画的部分,首先要介绍的是隐式动画。所谓隐式动画就是由系统自动完成的动画。 事务 Core An...
    Counting_S阅读 502评论 0 9
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,586评论 6 30
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,152评论 5 13
  • >*按照我的意思去做,而不是我说的。* --埃德娜,辛普森 我们在第一部分讨论了Core Animation除了动...
    夜空下最亮的亮点阅读 665评论 1 1
  • 前言 本文只要描述了iOS中的Core Animation(核心动画:隐式动画、显示动画)、贝塞尔曲线、UIVie...
    GitHubPorter阅读 3,669评论 7 11