Animation in IOS -- Core Animation

Source: Internet
Author: User

Animation in IOS -- Core Animation

I. Basic animation CABasicAnimation
 
1 // initialization method: CABasicAnimation * cabase = [CABasicAnimation animation]; 2 // you can use keyPath to set the attribute to be animated. In this example, it is set to bounds cabase. keyPath = @ "bounds"; 3 // use toValue to set the State cabase at the end of the animation. toValue = [NSValue valueWithCGRect: CGRectMake (0, 0, 10,100)]; // sets the range of cabase for each change through byValue. byValue = [NSValue valueWithCGRect: CGRectMake (0, 0, 10,100)]; // set the status cabase at the beginning. fromValue = [NSValue valueWithCGPoint: CGPointMake (0, 0)]; 4 // set the animation duration cabase. duration = 2; // Save the animated cabase. fillMode = kCAFillModeForwards; // Save the settings without canceling cabase. removedOnCompletion = NO; [_ layer addAnimation: cabase forKey: nil];

  Case: use basic animation to implement affine transform Animation

 
CABasicAnimation * cabase=[CABasicAnimation animation]; cabase.keyPath=@"transform"; cabase.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 2, 1)]; cabase.duration=2; cabase.fillMode=kCAFillModeForwards; cabase.removedOnCompletion=NO; [_layer addAnimation:cabase forKey:nil];
II. Key Frame Animation
 
1 // initialization method CAKeyframeAnimation * keyfram = [CAKeyframeAnimation animation]; 2 // you can use keyPath to set the animation attribute. Set it to position keyfram. keyPath = @ "position"; 3 // set the point that the animation needs to go through CGPoint p1 = CGPointZero; CGPoint p2 = CGPointMake (150, 0); CGPoint p3 = CGPointMake (150,150 ); CGPoint p4 = CGPointMake (0,150); CGPoint p5 = CGPointZero; NSValue * v1 = [NSValue valueWithCGPoint: p1]; NSValue * v2 = [NSValue valueWithCGPoint: p2]; NSValue * v3 = [NSValue valueWithCGPoint: p3]; NSValue * v4 = [NSValue valueWithCGPoint: p4]; NSValue * v5 = [NSValue valueWithCGPoint: p5]; 4 // Add the corresponding value to the animation and set the animation to retain keyfram. values = @ [v1, v2, v3, v4, v5]; keyfram. duration = 1; keyfram. fillMode = kCAFillModeForwards; keyfram. removedOnCompletion = NO; [_ layer addAnimation: keyfram forKey: nil];

Case: Image swing through Key Frame Animation

 

CAKeyframeAnimation * anima = [CAKeyframeAnimation animation]; // you can set the angle of the radiation transformation to achieve anima. keyPath = @ "transform. rotation "; float p1 = 4/180. 0 * M_PI; anima. duration = 0.2; anima. values = @ [@ (-p1), @ (p1), @ (-p1)]; anima. fillMode = kCAFillModeForwards; anima. removedOnCompletion = NO; anima. repeatCount = MAXFLOAT; [_ layer addAnimation: anima forKey: nil]; _ layer. transform = CATransform3DMakeRotation (M_PI, 0, 0, 0 );

 

 
3. Transition Animation
 
1 // initialization method CATransition * tran = [CATransition animation]; 2 // sets the animation effect tran. type = @ "rippleEffect"; // kCATransitionFade kCATransitionMoveIn kCATransitionPush kCATransitionReveal 3 // set the animation direction to tran. subtype = kCATransitionFromLeft; // animation direction kCATransitionFromRight kCATransitionFromLeft kCATransitionFromTop kCATransitionFromBottom 4 // set animation retention and animation duration tran. fillMode = kCAFillModeForwards; tran. removedOnCompletion = NO; tran. duration = 1; [self. myImageView. layer addAnimation: tran forKey: nil];
Iv. UIView encapsulation Animation

UIKit directly integrates the animation into the UIView class. When some internal attributes change, UIView provides animation support for these changes. The work required to execute the animation is automatically completed by the UIView class, but you still need to notify the view when you want to execute the animation. Therefore, you need to put the code for changing the attribute in [UIViewBeginAnimations: NilContext: Nil] and [UIViewCommitAnimations]

  1. Common Methods:

 
// Set the animation proxy + (void) setAnimationDelegate :( id) delegate // set the selector of the delegate object when the animation is about to begin, and set beginAnimations: context: the input parameters are passed into selector + (void) setAnimationWillStartSelector :( SEL) selector // call method + (void) setAnimationDidStopSelector :( SEL) when the animation ends) selector // sets the animation duration + (void) setAnimationDuration :( NSTimeInterval) duration // sets the animation latency + (void) setAnimationDelay :( NSTimeInterval) delay // sets the animation start time + (void) setAnimationStartDate :( NSDate *) startDate // set the animation rhythm + (void) setAnimationCurve :( UIViewAnimationCurve) curve // set the number of animation repetitions + (void) setAnimationRepeatCount :( float) repeatCount // if it is set to YES, it indicates that the animation will be executed repeatedly at the same time. + (void) setAnimationRepeatAutoreverses :( BOOL) repeatAutoreverses // you can specify the view transition effect, transition specifies the transition type. If the cache is set to YES, The view cache is used, with better performance + (void) setAnimationTransition :( UIViewAnimationTransition) transition forView :( UIView *) view cache :( BOOL) cache

2. Case studies

 

// Rotate the animation [UIView beginAnimations: @ "roate" context: nil]; [UIView setAnimationCurve: animation]; [UIView setAnimationDuration: 1.5]; [UIView setAnimationDelegate: self]; _ view. transform = CGAffineTransformRotate (_ view. transform, M_PI_2); [UIView setAnimationDidStopSelector: @ selector (endAnimate)]; [UIView commitAnimations]; // transition animation [UIView beginAnimations: @ "transition" context: nil]; [UIView setAnimationCurve: UIViewAnimationCurveLinear]; [UIView setAnimationTransition: Lateral forView: _ mainView cache: YES]; [UIView setAnimationDuration: 1.5]; NSInteger index1 = [_ mainView. subviews indexOfObject: _ view]; NSInteger index2 = [_ mainView. subviews indexOfObject: _ view2]; [_ mainView exchangeSubviewAtIndex: index1 withSubviewAtIndex: index2]; [UIView commitAnimations];

 

 

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.