Core Animation - 完成块

75 阅读1分钟

什么是完成块呢?这类似于我们使用block完成事务后的回调,在完成既定的事物后出发的某种操作,拿上一篇隐式动画中的改变颜色来说可以为他设定一个完成块,在颜色变换之后将色块旋转90度:

 //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].CGCo //commit the transaction
[CATransaction commit];

根据完成块概念,即完成颜色改变后触发,所以旋转动画出现在颜色入栈出栈后,同时由于用默认的事物做变换,未设置旋转动画的时间,所以默认为0.25s。
查看地址:github.com/codeliu6572…