IPhone Development Study Notes animation effects through UIView

Source: Internet
Author: User

IPhone DevelopmentLearning notes passedUIViewImplementationAnimationThe effect is what we will introduce in this article. By encapsulating Core Animation, UIKit implements some commonAnimationIt is very convenient to use. You can useUIViewTo declareAnimationBlock. Any attribute changes made in this block will be displayed.AnimationEffect.

There are two types of syntax. Here we talk about the old-fashioned method. For the new method after IOS4.0, refer to the document. The basic idea is the same. For details, refer to this:

 
 
  1. http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/AnimatingViews/AnimatingViews.html 

First, describe several basic concepts to facilitate understanding of the subsequent functions.

Attribute change: attributes that can realize the animation effect include position frame, bound), alignment, transparency, background color, content stretching, and transform. Here we will talk about this)

Timing curve: The time curve, with time as the horizontal axis, and other values (here referred to as the attribute to be changed) as the vertical axis. The function curve during the animation duration.

Slow in/slow out: slow in/slow out. Combined with the above concept of time curve, attribute changes will slow down at the animation start/end. See the figure below: the animation effect is not very good by default. You can find it on the Internet)

Liner: linear variation. This is not to mention. There are two kinds of time variation curves. The default value is EaseInEaseOut. Undoubtedly, the effect of EaseInEaseOut will be smoother, but the load will be higher, but it is generally not a problem.
Fade in/fade out: fade in, fade out, is an animated effect, that is, gradually disappear, and such a thing gradually appears.

Before talking about specific functions, let's take an example first,

Code

 
 
  1.     [UIView beginAnimations:@"ToggleViews" context:nil];      
  2.     [UIView setAnimationDuration:1.0];      
  3.    [UIView setAnimationCurve:UIViewAnimationEaseInOut];      
  4.    // Make the animatable changes.    firstView.alpha = 0.0;      
  5.    secondView.alpha = 1.0;      
  6.    // Commit the changes and perform the animation.      
  7.   [UIView commitAnimations]; 

This code can implement a nice fade-in and fade-out switch. All you need to do is use the begin/commit function to circle a region and write the changes you want to make, no matter how many, they will not be immediately executed, knowing that the commit function is committed. A simple description of the function:

BeginAnimation: context: Both parameters are used for delegate. Generally, nil is fine. The animationID indicates the name of the current animation. It is used for different purposes when a proxy corresponds to a multi-terminal animation, the context is void *, which is commonly used in callback functions. It is used to transmit additional data, save the context, and avoid using global variables.

SetAnimationCurve: As mentioned above, the default value is UIViewAnimationCurveEaseInOut. You can leave it empty.

SetAnimationDuration: Specifies the animation length, in seconds.

Add another common function, setAnimationRepeatCount: You can repeat the animation, which is quite useful in some scenarios.

If you need to perform operations before or after the animation, you can define the proxy as two callback functions ). Let's take a look at the example below. An animation is followed by another animation. If you are familiar with the use of the agent, you will not be able to talk about it.

Code

 
 
  1. // This method begins the first animation.  
  2. - (IBAction)showHideView:(id)sender{  
  3.     [UIView beginAnimations:@"ShowHideView" context:nil];      
  4.     [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];      
  5.     [UIView setAnimationDuration:1.0];      
  6.     [UIView setAnimationDelegate:self];      
  7.     [UIView setAnimationDidStopSelector:@selector(showHideDidStop:finished:context:)];       
  8.     // Make the animatable changes.    thirdView.alpha = 0.0;       
  9.     // Commit the changes and perform the animation.    [UIView commitAnimations];  
  10.   }   
  11.   // Called at the end of the preceding animation.  
  12.   - (void)showHideDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{      
  13.      [UIView beginAnimations:@"ShowHideView2" context:nil];      
  14.      [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];      
  15.      [UIView setAnimationDuration:1.0];      
  16.      [UIView setAnimationDelay:1.0];       
  17.      thirdView.alpha = 1.0;       
  18.      [UIView commitAnimations];} 

Next we will talk about the view transition animation effect, which is a common page flip effect. The old version is written like this:

Code

 
 
  1. // The code is excerpted from basic iphone Development
  2. [UIView setAnimationTransition:
  3. UIViewAnimationTransitionFlipFromRight
  4. ForView: self. view cache: YES];
  5. [BlueViewController viewWillAppear: YES];
  6. [YellowViewController viewWillDisappear: YES];
  7. [BlueViewController. view removeFromSuperview];
  8. [Self. view insertSubview: yellowViewController. view atIndex: 0];
  9. [YellowViewController viewDidDisappear: YES];
  10. [BlueViewController viewDidAppear: YES];

Before IOS4.0, to achieve the animation effect switching between views, you must use the parent view and then switch the child view. Only the effect of the Child view can be animated, so you can see the parent view written in forView in setAnimationTranistion.

After 4.0, you can write it like this:

Code

 
 
  1.     [UIView transitionFromView:(self.view)                         
  2.     toView:(self._view2)                         
  3.     duration:1.0                         
  4.     options:UIViewAnimationOptionTransitionCurlUp                         
  5.     completion:^(BOOL finished) {      
  6.   }     
  7. ]; 

Summary:IPhone DevelopmentLearning notes passedUIViewImplementationAnimationI hope this article will help you better understand the results!

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.