IOS Core Animation Advanced techniques-Implicit animation

Source: Internet
Author: User

On six chapters:

    1. Layer Tree
    2. Layer's Homestay Map
    3. Layer geometry
    4. Layer Visual effects
    5. Layer transformations
    6. Dedicated layers

This essay focuses on implicit animation of layers.

Implicit animations:

    • No animation type is specified, and a property is changed, and the Core animation determines how and when to animate.
    • The events performed by the animation depend on the settings of the current transaction;
    • The animation type depends on the layer behavior.
    • The Core animation assumes that anything on the screen can be animated, and that the default animation effect is turned on.
    • When an animated property of Calayer is changed, the default transitions smoothly from previous values to the new value instead of displaying the new value immediately on the screen, thus carrying an implicit animation.
    • Demonstration examples:
      • Mylayer=[calayer layer];
      • Mylayer.frame=cgrectmake (50.0f, 50.0f, 100.0f, 100.0f);
      • Mylayer.backgroundcolor=[uicolor Bluecolor]. Cgcolor;
      • [Myview.layer Addsublayer:mylayer];
      • -(void) changecolor{
      • CGFloat red = Arc4random ()/(cgfloat) Int_max;
      • CGFloat green = Arc4random ()/(cgfloat) Int_max;
      • CGFloat blue = Arc4random ()/(cgfloat) Int_max;
      • Mylayer.backgroundcolor = [Uicolor colorwithred:red green:green blue:blue alpha:1.0]. Cgcolor;
      • }

Transaction:

  • The Core animation is used to include a mechanism for animating the set of column properties, and changing the properties of the animated layer with the specified transaction does not change immediately, but instead begins with an animation transition to the new value when the transaction commits
  • The
  • transaction is managed through the Catransaction class:
    • The class has no properties or instance methods, cannot be created with +alloc and-init methods
    • can use +begin (a new transaction, corresponding to the animation method in UIView + Beginanimations:context:) and +commit (commit a transaction, corresponding to UIView in the +commitanimations animation method) respectively into the stack or out of the stack (iOS4, UIView animation method added + Animatewithduration:animations: Wrapping the begin with the commit method in the method)
    • can set the animation time of the current transaction through the +setanimationduration: method. For the purpose of not affecting other animations, it is common to use this method before it is set in a separate transaction (between Begin and commit)
    • can get the animated time value (default 0.25s)
    • example by using the +animationduration method:
      • -(void) changecolor{
      • [catransaction begin];
      • [Catransaction setanimationduration:1.0];
      • cgfloat red = Arc4random ()/(CGFloat) Int_max;
      • CGFloat green = Arc4random ()/(CGFloat) Int_max;
      • CGFloat blue = Arc4random ()/(CGFloat) Int_max;
      • Mylayer.backgroundcolor = [Uicolor colorwithred:red green:green blue:blue alpha:1.0]. Cgcolor;
      • [Catransaction commit];
      • }
  • Completion BLOCK:
    • Catranscation interface provides +setcompletionblock: Method implements the callback block block that animates the end of animation in the UIView
    • Demonstration examples:
      • -(void) changecolor{
      • [Catransaction begin];
      • [Catransaction setanimationduration:1.0];
      • [Catransaction setcompletionblock:^{
      • Cgaffinetransform transform = self.colorLayer.affineTransform;
      • Transform = Cgaffinetransformrotate (transform, m_pi_2);
      • Mylayer.affinetransform = transform;
      • }];
      • CGFloat red = Arc4random ()/(cgfloat) Int_max;
      • CGFloat green = Arc4random ()/(cgfloat) Int_max;
      • CGFloat blue = Arc4random ()/(cgfloat) Int_max;
      • Mylayer.backgroundcolor = [Uicolor colorwithred:red green:green blue:blue alpha:1.0]. Cgcolor;
      • [Catransaction commit];
      • }

Layer behavior:

    • Behavior:
      • Calayer Auto-applied animations when Calayer properties change
      • When the Calayer property is changed, the-actionforkey: method is called, passing the name of the property
      • In essence, the following steps are:

1.CALayer detects if it has a delegate, and if it implements the-actionforlayer:forkey method specified by the Calayerdelegate protocol, then directly invokes and returns the result

2. If no delegate or delegate does not implement the-actionforlayer:forkey method, Calayer then examines the actions dictionary that contains the property name corresponding to the behavior map

3. If the action dictionary does not contain a corresponding attribute, then Calayer then searches its style dictionary for the property name

4. If the corresponding behavior is not found in the style, then the layer will invoke the-defaultactionforkey that defines the standard behavior of each property directly: method

5. After the end of the search,-actionforkey: Either return empty (no animation), or the object that corresponds to the Caaction protocol, and finally calayer the result to animate the old value of the property and the new value.

    • The above is a separate layer to make the property changes, you will see the occurrence of implicit animation,
    • If you make a property change directly to the layer associated with UIView, the result on the screen will be to switch to the new value immediately instead of the previous smooth transition animation
    • Because UIView the implicit animation feature of the associated layer is turned off.
    • Each uiview plays a delegate to its associated layer and provides an implementation of-actionforlayer:forkey, and when not in the implementation of an animation block, UIView returns nil for all layer behavior, and returns a non-null value if within the animation block range
    • Demonstration Example: (UIView's actionforlayer:forkey: implementation)
      • -(void) viewdidload{
      • [Super Viewdidload];
      • NSLog (@ "Outside:%@", [Self.layerview actionForLayer:self.layerView.layer forkey:@ "BackgroundColor"]);
      • [UIView Beginanimations:nil Context:nil];
      • NSLog (@ "Inside:%@", [Self.layerview actionForLayer:self.layerView.layer forkey:@ "BackgroundColor"]);
      • [UIView commitanimations];
      • }
      • /* Program Run Result:
      • $ layertest[21215:c07] Outside: <null>
      • $ layertest[21215:c07] Inside: <CABasicAnimation:0x757f090>
      • Conclusion: When a property changes outside the animation block, UIView directly disables implicit animation by returning nil. But if within the range of the animation block, the corresponding property is returned according to the animation specific type, in this example is Cabasicanimation* /
    • Above by setting up the proxy and implementing the Proxy method returns nil disable Calayer implicit animation
    • There is another way to disable this:
      • Turn implicit animations on or off for all properties via Catransaction's +setdisableactions: Method
      • such as: [Catransaction Setdisableactions:yes];
      • Summary:
      • The 1.UIView associated layer disables implicit animation, and the only way to animate such a layer is to use UIView's animated function (instead of relying on catransaction), or to inherit UIView, overriding-actionforlayer:forkey: Method, Or create a display animation directly
      • 2. For a separate layer, you can control the implicit animation by implementing the layer's-actionforlayer:forkey: Delegate method, or by providing an actions dictionary
    • To change the default implicit animation implementation custom behavior:
      • By setting a custom actions dictionary for the standalone layer Calayer, you can also use a delegate implementation, but the actions dictionary can write less code
      • Use example:
        • self.colorlayer = [Calayer layer];
        • self.colorLayer.frame = CGRectMake (50.0f, 50.0f, 100.0f, 100.0f);
        • Self.colorLayer.backgroundColor = [Uicolor Bluecolor]. Cgcolor;
        • //add a custom action
        • catransition *transition = [catransition animation];
        • transition.type = Kcatransitionpush;
        • transition.subtype = kcatransitionfromleft;
        • self.colorLayer.actions = @{@ "BackgroundColor": transition};//focus
        • //add it to our view
        • [SELF.L Ayerview.layer AddSublayer:self.colorLayer];
        • -(ibaction) changecolor{
        • //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;
        • }
  • Rendering with model:
    • When the property of Calayer is changed, the property value is updated immediately, but the screen does not change immediately.
    • This is because the properties you set do not directly adjust the appearance of the layer, only the appearance that will change after the end of the layer animation is defined
    • Calayer properties, like models; the Core animation plays a controller; The property is displayed as a view on the screen, that is, the relationship between the two is a miniature MVC pattern
    • During an animation transition, you need to record a property value for the current progress of the transition process, which is stored in a separate layer called the rendering layer.
    • Rendering layers can be accessed through the-presentationglayer method;
    • You can get the values that are actually displayed on the current screen by rendering the layer's values.
    • The rendering layer can return the calayer that is being rendered by-modellayer, typically called on a layer-modellayer returns-self
    • Rendering a layer generally does not require management, but in both cases it becomes useful to render the layer:
      • 1. Synchronizing animations
      • 2. Handle user interaction (you can use the-hittest: method to determine whether a specified layer is touched, this is called-hittest on the rendering layer instead of the model layer: it makes more sense because the rendering layer represents the position of the layer that the user is currently seeing, not the position after the end of the current animation)
      • Example of Scenario 2: (Tapping anywhere on the screen will cause the layer to pan there.) Click on the layer itself to randomly change its color. By invoking-hittest on the rendering layer: To determine if it is clicked,
        • Self.colorlayer = [Calayer layer];
        • Self.colorLayer.frame = CGRectMake (0, 0, 100, 100);
        • Self.colorLayer.position = Cgpointmake (SELF.VIEW.BOUNDS.SIZE.WIDTH/2, SELF.VIEW.BOUNDS.SIZE.HEIGHT/2);
        • Self.colorLayer.backgroundColor = [Uicolor Redcolor]. Cgcolor;
        • [Self.view.layer AddSublayer:self.colorLayer];
        • -(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{
        • Get the touch point
        • Cgpoint point = [[touches anyobject] locationInView:self.view];
        • Check if we ' ve tapped the moving layer
        • if ([Self.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;
        • Self.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];
        • Self.colorLayer.position = point;
        • [Catransaction commit];
        • }
        • }
        • /*
        • If you modify the code so that-hittest: directly acting on colorlayer instead of rendering the layer, you will find that it does not display correctly when the layer moves.
        • This is where you need to click where the layer will be moved instead of the layer itself to respond to the click (That's why you're using the rendering layer to respond to the interaction).
        • */



IOS Core Animation Advanced techniques-Implicit animation

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.