基于UIView的block的动画容许你在动画结束的时候提供一个完成的动做。CATranscation接口提供的+setCompletionBlock:方法也有一样的功能。咱们来调整上个例子,在颜色变化结束以后执行一些操做。咱们来添加一个完成以后的block,用来在每次颜色变化结束以后切换到另外一个旋转90的动画。代码见清单7.3,运行结果见图7.2。 git
清单7.3 在颜色动画完成以后添加一个回调 github
- (IBAction)changeColor { //begin a new transaction [CATransaction begin]; //set the animation duration to 1 second [CATransaction setAnimationDuration:1.0]; //add the spin animation on completion [CATransaction setCompletionBlock:^{ //rotate the layer 90 degrees CGAffineTransform transform = self.colorLayer.affineTransform; transform = CGAffineTransformRotate(transform, M_PI_2); self.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; self.colorLayer.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor; //commit the transaction [CATransaction commit]; }
图7.2 颜色渐变之完成以后再作一次旋转 dom
注意旋转动画要比颜色渐变快得多,这是由于完成块是在颜色渐变的事务提交并出栈以后才被执行,因而,用默认的事务作变换,默认的时间也就变成了0.25秒。 ide
如今来作个实验,试着直接对UIView关联的图层作动画而不是一个单独的图层。清单7.4是对清单7.2代码的一点修改,移除了colorLayer,而且直接设置layerView关联图层的背景色。 函数
清单7.4 直接设置图层的属性 测试
@interface ViewController () @property (nonatomic, weak) IBOutlet UIView *layerView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //set the color of our layerView backing layer directly self.layerView.layer.backgroundColor = [UIColor blueColor].CGColor; } - (IBAction)changeColor { //begin a new transaction [CATransaction begin]; //set the animation duration to 1 second [CATransaction setAnimationDuration:1.0]; //randomize the layer background color CGFloat red = arc4random() / (CGFloat)INT_MAX; CGFloat green = arc4random() / (CGFloat)INT_MAX; CGFloat blue = arc4random() / (CGFloat)INT_MAX; self.layerView.layer.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor; //commit the transaction [CATransaction commit]; }
运行程序,你会发现当按下按钮,图层颜色瞬间切换到新的值,而不是以前平滑过渡的动画。发生了什么呢?隐式动画好像被UIView关联图层给禁用了。 动画
试想一下,若是UIView的属性都有动画特性的话,那么不管在何时修改它,咱们都应该能注意到的。因此,若是说UIKit创建在Core Animation(默认对全部东西都作动画)之上,那么隐式动画是如何被UIKit禁用掉呢? atom
咱们知道Core Animation一般对CALayer的全部属性(可动画的属性)作动画,可是UIView把它关联的图层的这个特性关闭了。为了更好说明这一点,咱们须要知道隐式动画是如何实现的。 spa
咱们把改变属性时CALayer自动应用的动画称做行为,当CALayer的属性被修改时候,它会调用-actionForKey:方法,传递属性的名称。剩下的操做都在CALayer的头文件中有详细的说明,实质上是以下几步: code
因此一轮完整的搜索结束以后,-actionForKey:要么返回空(这种状况下将不会有动画发生),要么是CAAction协议对应的对象,最后CALayer拿这个结果去对先前和当前的值作动画。
因而这就解释了UIKit是如何禁用隐式动画的:每一个UIView对它关联的图层都扮演了一个委托,而且提供了-actionForLayer:forKey的实现方法。当不在一个动画块的实现中,UIView对全部图层行为返回nil,可是在动画block范围以内,它就返回了一个非空值。咱们能够用一个demo作个简单的实验(清单7.5)
清单7.5 测试UIView的actionForLayer:forKey:实现
@interface ViewController () @property (nonatomic, weak) IBOutlet UIView *layerView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //test layer action when outside of animation block NSLog(@"Outside: %@", [self.layerView actionForLayer:self.layerView.layer forKey:@"backgroundColor"]); //begin animation block [UIView beginAnimations:nil context:nil]; //test layer action when inside of animation block NSLog(@"Inside: %@", [self.layerView actionForLayer:self.layerView.layer forKey:@"backgroundColor"]); //end animation block [UIView commitAnimations]; } @end
运行程序,控制台显示结果以下:
$ LayerTest[21215:c07] Outside: <null> $ LayerTest[21215:c07] Inside: <CABasicAnimation: 0x757f090>
因而咱们能够预言,当属性在动画块以外发生改变,UIView直接经过返回nil来禁用隐式动画。但若是在动画块范围以内,根据动画具体类型返回相应的属性,在这个例子就是CABasicAnimation(第八章“显式动画”将会提到)。
固然返回nil并非禁用隐式动画惟一的办法,CATransacition有个方法叫作 +setDisableActions: ,能够用来对全部属性打开或者关闭隐式动画。若是在清单7.2的[CATransaction begin]以后添加下面的代码,一样也会阻止动画的发生:
[CATransaction setDisableActions:YES];
总结一下,咱们知道了以下几点