Transaction (implicit animation), transaction Animation

Source: Internet
Author: User

Transaction (implicit animation), transaction Animation
Transactions

Core Animation is based on a hypothesis that everything on the screen can be animated (or possibly. You do not need to manually open an Animation in Core Animation. Instead, you need to disable it explicitly. Otherwise, the Animation will always exist.

When you changeCALayerIs an animation property, it cannot be reflected on the screen. Instead, it is a smooth transition from the previous value to the new value. All of this is the default action. You do not need to perform any additional operations.

This looks great, and it doesn't seem very real. Let's use a demo to explain: first, create a blue square like the "layer Tree" in the first chapter, and then add a button, randomly change its color. For the code, see list 7.1. Click the button and you will find that the color of the layer is smoothly transitioned to a new value, instead of a hop (Figure 7.1 ).

Listing 7.1 randomly changing the layer color

1 @ interface ViewController () 2 3 @ property (nonatomic, weak) IBOutlet UIView * layerView; 4 @ property (nonatomic, weak) IBOutlet CALayer * colorLayer; /* the enthusiastic person finds that this should be changed to @ property (nonatomic, strong) CALayer * colorLayer; otherwise, the running result is incorrect. 5 */6 @ end 7 8 @ implementation ViewController 9 10-(void) viewDidLoad11 {12 [super viewDidLoad]; 13 // create sublayer14 self. colorLayer = [CALayer layer]; 15 self. colorLayer. frame = CGRectMake (501_f, 501_f, 1001_f, 1001_f); 16 self. colorLayer. backgroundColor = [UIColor blueColor]. CGColor; 17 // add it to our view18 [self. layerView. layer addSublayer: self. colorLayer]; 19} 20 21-(IBAction) changeColor22 {23 // randomize the layer background color24 CGFloat red = arc4random ()/(CGFloat) INT_MAX; 25 CGFloat green = arc4random () /(CGFloat) INT_MAX; 26 CGFloat blue = arc4random ()/(CGFloat) INT_MAX; 27 self. colorLayer. backgroundColor = [UIColor colorWithRed: red green: green blue: blue alpha: 1.0]. CGColor; 000028} 29 30 @ end

 

Figure 7.1 Add a button to control the layer color

This is actually calledImplicitAnimation. It is called implicit because we have not specified any animation type. We only changed one attribute, and then Core Animation was used to determine how and when to perform the Animation. Core Animaiton is also supportedExplicitAnimation, which is described in the following chapter.

But when you change an attribute, how does Core Animation determine the Animation type and duration? The actual animation execution time depends on the currentTransactionsThe animation type depends onLayer behavior.

A transaction is actually a mechanism used by Core Animation to include a series of property Animation sets. Any layer attribute that can be animated with a specified transaction will not change immediately, but once the transactionSubmitStart to use an animation to transition to a new value.

The transaction isCATransactionClass for Management. The design of this class is a bit strange. Unlike you manage a simple transaction as expected by its naming, it manages a stack of transactions that you cannot access.CATransactionNo attribute or instance method, and cannot be used+allocAnd-initMethod. However+beginAnd+commitRespectively.

Any layer attribute that can be animated will be added to the top of the stack.+setAnimationDuration:Method To set the animation time of the current transaction, or+animationDurationMethod to obtain the value (the default value is 0.25 seconds ).

Core Animation in eachRun loopA new transaction is automatically started in a cycle (run loop is something iOS collects user input, processes timer or network events, and redraws the screen), even if you do not explicitly use[CATransaction begin]When a transaction starts, any changes to the attributes in a run loop will be centralized and then an animation of 0.25 seconds will be made.

After understanding this, we can easily modify the color-changing animation time. Of course we can use the current transaction's+setAnimationDuration:Method to modify the animation time, but here we start a new transaction, so the modification time will not have any other side effects. Because modifying the time of the current transaction may lead to other animations at the same time point (such as screen rotation), it is best to press a new transaction before adjusting the animation.

The modified code can be found in the configuration in listing 7.2. Run the program and you will find that the color block is slower than before.

Usage in listing 7.2CATransactionControl animation time

 1 - (IBAction)changeColor 2 { 3     //begin a new transaction 4     [CATransaction begin]; 5     //set the animation duration to 1 second 6     [CATransaction setAnimationDuration:1.0]; 7     //randomize the layer background color 8     CGFloat red = arc4random() / (CGFloat)INT_MAX; 9     CGFloat green = arc4random() / (CGFloat)INT_MAX;10     CGFloat blue = arc4random() / (CGFloat)INT_MAX;11     self.colorLayer.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor;12     //commit the transaction13     [CATransaction commit];14 }

 

If you have usedUIViewThe animation method has made some animation effects, so we should be familiar with this mode.UIViewThere are two methods,+beginAnimations:context:And+commitAnimations, AndCATransactionOf+beginAnd+commitThe method is similar. In fact+beginAnimations:context:And+commitAnimationsAll views or layer attributes are changed.CATransaction.

In iOS4, Apple added a block-based animation method to UIView:+animateWithDuration:animations:. In this way, writing a bunch of property animations will be easier in syntax, but they are basically doing the same thing.

CATransactionOf+beginAnd+commitMethod in+animateWithDuration:animations:It is automatically called internally, so that all attribute changes in the block will be included by the firm. In this way, developers can avoid+beginAnd+commitRisks Caused by mismatching.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.