IOS animation implementation Summary

Source: Internet
Author: User

IOS animation implementation Summary
In iOS, there are two types of animation implementation directions: one is the animation method that operates UIView, and the other is the core animation, but in iOS 7, UIView is also involved with core animations. Method 1 (add an animation using the core animation) the hierarchical relationship transition animation (CATransition) of the core animation is used for scene conversion animation, which can even provide the layer with animation effects such as screen removal and screen animation. UINavigationController implements the animation effect of the teacher and apprentice who talk about the Controller pushing into the screen through CATransition. Common property type: animation excessive type subtype: animation excessive direction startProgress: animation starting point (in the overall percentage) (available value from 0 to 1, the duration of the start or end in the animation, the start time must be smaller than the end time, the same below) endProgress: animation end point (percentage in the overall animation)

CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];  baseAnimation.fromValue = [NSValue valueWithCGRect:CGRectMake(20, 20, 100, 100)];  baseAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(100, 100, 200, 200)];  baseAnimation.duration = 2.0;  baseAnimation.removedOnCompletion = NO;  baseAnimation.fillMode = kCAFillModeForwards;  baseAnimation.repeatCount = MAXFLOAT;  [self.myView.layer addAnimation:baseAnimation forKey:nil];

 

CABasicAnimation is a subclass of CAPropertyAnimation. An animation can control the change of an attribute. The change value can only be changed between two values. You can set it in fromValue and toValue.
CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];  baseAnimation.fromValue = [NSValue valueWithCGRect:CGRectMake(20, 20, 100, 100)];  baseAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(100, 100, 200, 200)];  baseAnimation.duration = 2.0;  baseAnimation.removedOnCompletion = NO;  baseAnimation.fillMode = kCAFillModeForwards;  baseAnimation.repeatCount = MAXFLOAT;  [self.myView.layer addAnimation:baseAnimation forKey:nil];

 

Frame Animation is also a subclass of CAPropertyAnimation. Therefore, it controls the attributes of a view for animation. Unlike CABaseAnimation, CAKeyframeAnimation can add multiple key frames, CABaseAnimation can be seen as a frame animation for two key frames. We can make good use of the key frames of the frame animation for interesting animations, such as bubble effects.
CAAnimationGroup * group = [[CAAnimationGroup alloc] init]; // displacement CAKeyframeAnimation * positionAnima = [CAKeyframeAnimation animationWithKeyPath: @ "position"]; positionAnima. calculationMode = kCAAnimationCubicPaced; positionAnima. duration = 5; positionAnima. fillMode = kCAFillModeForwards; positionAnima. removedOnCompletion = NO; positionAnima. repeatCount = MAXFLOAT; positionAnima. timingFunction = [CAMediaTimingFunction functionWithName: callback]; // Add the mobile path CGMutablePathRef curvedPath = CGPathCreateMutable (); CGRect circleContainer = CGRectInset (myView. frame, myView. frame. size. width/2-3, myView. frame. size. height/2-3); CGPathAddEllipseInRect (curvedPath, NULL, circleContainer); positionAnima. path = curvedPath; CGPathRelease (curvedPath); [myView. layer addAnimation: positionAnima forKey: nil]; // zooming X CAKeyframeAnimation * scaleX = [CAKeyframeAnimation animationWithKeyPath: @ "transform. scale. x "]; scaleX. duration= 1.0; scaleX. values = @ [@ 1.0, @ 1.1, @ 1.0]; scaleX. keyTimes = @ [@ 0.0, @ 0.5, @ 1.0]; scaleX. repeatCount = MAXFLOAT; scaleX. autoreverses = YES; scaleX. timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseOut]; [myView. layer addAnimation: scaleX forKey: nil]; // scale Y CAKeyframeAnimation * scaleY = [CAKeyframeAnimation animationWithKeyPath: @ "transform. scale. y "]; scaleY. duration= 1.5; scaleY. values = @ [@ 1.0, @ 1.1, @ 1.0]; scaleY. keyTimes = @ [@ 0.0, @ 0.5, @ 1.0]; scaleY. repeatCount = MAXFLOAT; scaleY. autoreverses = YES; group. animations = @ [positionAnima, scaleX, scaleY]; scaleY. timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseOut]; [myView. layer addAnimation: scaleY forKey: nil];

 

The running effect is because my computer is a black apple, so it will be done. Haha, white Apple should not be like this. A subclass of CAAnimationGroup (CAAnimation), which can save a group of animation objects. After CAAnimationGroup objects are added to a layer, all animations in the group can run at the same time, when multiple animations are required and the execution time is different, the animation group is not applicable. For example, the above bubble effect. Group. animations = [place an animation object in it]; method 2 (add an animation using UIView) UIView animation (hand code) add a single Animation
[UIView beginAnimations:nil context:nil];[UIView setAnimationDuration:4];CGPoint point = self.myView.center;point.y += 150;[self.myView setCenter:point];[UIView commitAnimations];

 

Add multiple animations
  [UIView beginAnimations:nil context:nil];    [UIView setAnimationDuration:4];    CGPoint point = self.myView.center;    point.y += 150;    [self.myView setCenter:point];    [UIView commitAnimations];    [UIView beginAnimations:nil context:nil];    [UIView setAnimationDuration:4];    [self.myView setAlpha:0.1];    [UIView commitAnimations];

 

UIView animation (Block) [UIView animateWithDuration: 4 animations: ^ {CGPoint point = self. myView. center; point. y + = 150; [self. myView setCenter: point];}]; UIView animation (Block Frame Animation), which is enabled from iOS7. Apple provides a convenient way to call Frame Animation without creating Frame Animation instances, directly control layer attributes.
[UIView animateKeyframesWithDuration:0.5 delay:1 options:UIViewKeyframeAnimationOptionAutoreverse animations:^{        self.view.bounds = CGRectMake(30, 30, 30, 30);    } completion:^(BOOL finished) {    }];

 

UIView transfer animation. + (Void) transitionFromView :( UIView *) fromView toView :( UIView *) toView duration :( NSTimeInterval) duration options :( UIViewAnimationOptions) options completion :( void (^) (BOOL finished )) the completion method should not be easy to understand. Simply put, after the method is called, it is equivalent to executing two sentences of code,
// Add toView to parent view [fromView. superview addSubview: toView]; // remove fromView from the parent view. superview removeFromSuperview];-duration: animation duration-options: Transfer animation type-animations: place the code that changes the view attribute in this block-completion: The block is automatically called after the animation ends.

 

 

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.