Introduction: In the development of APP, we will often use the animation effect. Using animation can make our APP cooler and more glamorous. The most important thing is to optimize user experience, but it depends on the quality of animation. Like QQ, wechat, Sina Weibo and other apps, the animation effect is very good, at least I like their animation, let me use the feeling is very smooth, the mood is very cheerful. This article will introduce the implementation method of UIView effect, non-core animation.

First, use THE UIView class for animation

In basic writing, the code must be placed between Begin and Commit: [UIView beginAnimations:nil Context :nil]; // start animation // Code… [UIView commitAnimations]; // Submit animations for a simple example: [UIView beginAnimations:nil Context :nil]; / / animation [UIView setAnimationDuration: 10.0]; CGPoint point = _imageview.center; point.y += 150; [_imageView setCenter:point]; [UIView commitAnimations]; // Submit animations to run multiple animations simultaneously: [UIView beginAnimations:nil Context :nil]; [UIView setAnimationDuration: 3.0]; [_imageView setAlpha: 0.0]; [UIView commitAnimations]; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration: 3.0]; CGPoint point = _imageView.center; point.y += 150; [_imageView setCenter:point]; [UIView commitAnimations]; The above code implements the animation effect (simultaneous execution) :

1. The image was shifted down 150 images

2. Set the image transparency to 0.

Specify the context: CGContextRef context = UIGraphicsGetCurrentContext (); [UIView beginAnimations:nil context:context]; [UIView setAnimationDuration: 2.0]; [_imageView setAlpha:0]; [UIView commitAnimations]; UIGraphicsGetCurrentContext () : access to the context of the current view

Other methods and properties:

+ (void)beginAnimations:(NSString *)animationID Context :(void *)context; // Start animation + (void)commitAnimations; + (void)setAnimationCurve:(UIViewAnimationCurve)curve; // Set the animation curve, default is uniform: + (void)setAnimationDuration:(NSTimeInterval)duration; + (void)setAnimationsEnabled:(BOOL)enabled; // The default is YES. When it is NO, skip the animation effect and jump directly to the post-execution state. + (void)setAnimationDelay:(NSTimeInterval)delay; + (void)setAnimationRepeatCount:(float)repeatCount; Continues to play number + / / animation (void) setAnimationRepeatAutoreverses (BOOL) repeatAutoreverses; // If YES, reverse the animation effect and return to the state before the animation reverse; The default is NO: + (void)setAnimationDelegate:(id)delegate; / / set the animation agent + (void) setAnimationWillStartSelector: (SEL) the selector; / / the animation will start execution method of xx (agents) must be set first animation: + (void) setAnimationDidStopSelector: (SEL) the selector; // Execute method ×× (animation agent must be set first) : /** *@param Cache If YES, the start and end views render once and create frames in the animation * otherwise, the view will render each frame. For example, you don’t need to constantly update in view transitions, You just need to wait for conversion to complete to update the view / + (void) setAnimationTransition: (UIViewAnimationTransition) transition forView (UIView *) view cache:(BOOL)cache; – (void)removeAllAnimations; Layer removeAllAnimations [_imageView.layer removeAllAnimations];

Use UIView animation block code:

[UIView animateWithDuration:4.0 // Animations :^{// code}]; Method 2: [UIView animateWithDuration:4.0 // Animations :^{// code…} completion:^(BOOL finished) { }]; Method 3:

[UIView animateWithDuration: 4.0 / / the animation time delay: 2.0 / / animation delay options: UIViewAnimationOptionCurveEaseIn / / animation transition effects Animations :^{// code…} completion:^(BOOL finished) {// Completed animations // code…}]; Method 4: Spring Animationring Animation

Since IOS7, Spring Animation has been widely used for system Animation effects: [UIView animateWithDuration: 4.0 / / the animation time delay: 0.0 / / animation delay usingSpringWithDamping: 1.0 / / similar to the spring vibration effect 0 ~ 1 InitialSpringVelocity: 5.0 / / velocity options: UIViewAnimationOptionCurveEaseInOut / / animation transition effects animations: ^ {/ / code… CGPoint point = _imageView.center; point.y += 150; [_imageView setCenter:point]; } completion:^(BOOL finished) {// Code… [_imageView setAlpha:1];}]; UsingSpringWithDamping: its range is 0.0f to 1.0f, and the smaller the value is, the more obvious the vibration effect of “spring” will be.

InitialSpringVelocity: The initial velocity, the higher the value, the faster the initial move. It is worth noting that when the initial speed is high and the time is short, there will also be a rebound.

Spring Animation is an ideal alternative to linear or ease-out Animation. Since Spring Animation is widely used in iOS, users are used to this Animation effect, so using it makes the App feel more natural, “INSTANTLY familiar” in Apple’s words. In addition, Spring Animation is not limited to locations; it applies to all properties that can be animated. Method 5, keyframe animation:

  • Add a keyframe method
  • @param duration Animation duration
  • @param delay Animation delay
  • @param Options Animation options
  • @Param Animations Executes the code
  • @param completion Animation completes the execution of the code+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewKeyframeAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;Add keyframe method:
  • Add keyframes
  • @param frameStartTime The relative start time of the animation
  • @param frameDuration Relative duration of animation
  • @Param Animations Executes the code

+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime relativeDuration:(double)frameDuration animations:(void (^)(void))animations; By relative time, I mean, “they automatically match their running time to the total duration of the animation.” Here is a simple example of a rainbow change view: Void (^keyFrameBlock)() = ^(){NSArray *arrayColors = @[[UIColor orangeColor], [UIColor yellowColor], [UIColor greenColor], [UIColor blueColor], [UIColor purpleColor], [UIColor redColor]]; NSUInteger colorCount = [arrayColors count]; For (NSUInteger I = 0; i < colorCount; i++) { [UIView addKeyframeWithRelativeStartTime:i / (CGFloat)colorCount relativeDuration:1 / (CGFloat)colorCount animations:^{ [_graduallyView setBackgroundColor:arrayColors[i]]; }]; }}; [UIView animateKeyframesWithDuration: delay 4.0:0.0 options: UIViewKeyframeAnimationOptionCalculationModeCubic | UIViewAnimationOptionCurveLinear animations: keyFrameBlock completion: ^ (BOOL finished) {/ / animation after the completion of the execution / / code…}]; Animation Transition effects (Options), added the following: UIViewKeyframeAnimationOptionCalculationModeLinear = 0 << 10, // default UIViewKeyframeAnimationOptionCalculationModeDiscrete = 1 << 10, UIViewKeyframeAnimationOptionCalculationModePaced = 2 << 10, UIViewKeyframeAnimationOptionCalculationModeCubic = 3 << 10, UIViewKeyframeAnimationOptionCalculationModeCubicPaced = 4 < < 10 below we look at a picture, let us easier to understand:

Summary:

There are many ways that UIView can animate. For simple animations you can throw them away. For more complex animations you can use the KeyFrame KeyFrame method.

As for the selection of which, you need to judge according to product demand.


Blog by GarveyCalvin

Post the source: http://www.cnblogs.com/GarveyCalvin/ link to blog post: http://www.cnblogs.com/GarveyCalvin/p/4193963.html blog copyright belongs to the author in this paper, and gardens of, welcome to reprint, but must keep the following statement, and give the original links, thank you for your cooperation!