Core Animation 1, coreanimation

Source: Internet
Author: User

Core Animation 1, coreanimation

1 // use the view as an attribute to facilitate multiple different animations. 2 _ myView = [[UIView alloc] init]; 3 _ myView. layer. position = CGPointMake (100,100); 4 _ myView. layer. bounds = CGRectMake (0, 0,100,100); 5 _ myView. backgroundColor = [UIColor blueColor]; 6 [self. view addSubview: _ myView]; 7 [_ myView release];

 

1 // create an animation object of the CABasicAnimation type and execute an animation for the position attribute of CALayer. 2 CABasicAnimation * anim = [CABasicAnimation animationWithKeyPath: @ "position"]; 3 4 // animation lasts 1.5 s 5 anim. duration = 1.5; 6 7 // position value gradient from (50, 80) to (300,350) 8 anim. fromValue = [NSValue valueWithCGPoint: CGPointMake (50, 80)]; 9 anim. toValue = [NSValue valueWithCGPoint: CGPointMake (300,350)]; 10 11 // set the animation proxy 12 anim. delegate = self; 13 14 // keep the animation status after execution 15 anim. removedOnCompletion = NO; 16 anim. fillMode = kCAFillModeForwards; 17 18 // Add the animation object to the layer of myView. 19 [_ myView. layer addAnimation: anim forKey: @ "translate"];
  • The translation animation is implemented above.
  • To achieve different effects, the most important thing is to set the keyPath in the initialization method of the CABasicAnimation object of the 2nd line. There are different keypaths in iOS to view API documentation search

    CABasicAnimation animationWithKeyPath Types

  • Rows 8th and 9 receive id-type parameters. Therefore, you cannot directly use the CGPoint struct type. Instead, you must package it into an NSValue object before using it.
  • Note: You can try to replace the toValue of row 9th with byValue.Difference: beforeTo the specified location, AfterWhat is the increase in the current position?.

  • By default, after the animation is executed, the animation is automatically removed from CALayer and CALayer returns to its original state. To maintain the animation status after execution, you can add 15th or 16 lines of code.

    FillMode is used to determine the behavior of the current object over a non-active period. For example, before the animation starts, after the animation ends. If it is an animation CAAnimation, you need to set its removedOnCompletion to NO, otherwise fillMode does not work.

    The significance of each fillMode
    KCAFillModeRemovedThis is the default value. That is to say, before and after the animation starts, the animation has no effect on the layer. After the animation ends, the layer will be restored to the previous state.
    KCAFillModeForwardsAfter the animation ends, the layer remains in the final state of the animation.
    KCAFillModeBackwardsThis is relative to kCAFillModeForwards, that is, before the animation starts, you only need to add the animation to a layer, and the layer immediately enters the initial state of the animation and waits until the animation starts. you can set the test code to add an animation to a layer with a latency of 5 seconds. then we will find that when the animation is not started, as long as the animation is added to the layer, the layer is in the initial state of the animation.
    KCAFillModeBothAfter understanding the two above, this is a good understanding. This is actually the synthesis of the two above. before the animation starts, the layer is in the initial state of the animation. After the animation ends, the layer remains in the final state of the animation.

  • The @ "translate" behind the row 19th only gives the animation object a name, or you can directly give it a nil. However, for convenience, you can call the removeAnimationForKey of CALayer: method to remove an animation based on the animation name
  • The self behind the row 12th is the view controller. Proxy implementation method:
1 # pragma mark ----- animation start ----- 2-(void) animationDidStart :( CAAnimation *) anim {3 NSLog (@ "animation started "); 4} 5 6 # pragma mark ----- animation end ----- 7-(void) animationDidStop :( CAAnimation *) anim finished :( BOOL) flag {8 // view the position value after the animation is executed 9 NSString * string = NSStringFromCGPoint (_ myView. layer. position); 10 NSLog (@ "the animation is finished, position: % @", string); 11}

In essence, the attribute values of layers are the initial values before the animation is executed, and are not actually changed.

CABasicAnimation * anim = [CABasicAnimation animationWithKeyPath: @ "transform"];
Anim. duration = 1;

CATransform3D form = CATransform3DMakeTranslation (350,350, 0 );
Anim. toValue = [NSValue valueWithCATransform3D: form];

[_ MyView. layer addAnimation: anim forKey: nil];

 

CABasicAnimation * anim = [CABasicAnimation animationWithKeyPath: @ "bounds"];
Anim. duration = 2;

Anim. toValue = [NSValue valueWithCGRect: CGRectMake (0, 0, 30, 30)];

[_ MyView. layer addAnimation: anim forKey: nil];

 

CABasicAnimation * anim = [CABasicAnimation animationWithKeyPath: @ "transform"];
Anim. duration = 1.5; // animation lasts 1.5 s

// The CALayer width is changed from 0.5 times to 2 times.
// The CALayer height is changed from 0.5 times to 1.5 times
Anim. fromValue = [NSValue valueWithCATransform3D: CATransform3DMakeScale (0.5, 0.5, 1)];
Anim. toValue = [NSValue valueWithCATransform3D: CATransform3DMakeScale (2, 1.5, 1)];

[_ MyView. layer addAnimation: anim forKey: nil];

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.