IOS animation effects and Implementations

Source: Internet
Author: User

Animation effects provide a smooth user experience during status or page conversion. In iOS, we do not need to write the animation code by ourselves, core Animation provides a variety of APIs to achieve the Animation effect you need. UIKit only uses UIView to display animations. animations support changes to these attributes under UIView: frame bounds center transform alpha backgroundColor contentStretch 1. Use UIView animation in commitAnimations mode [cpp]-(void) viewDidLoad {[super viewDidLoad]; UIButton * button = [UIButton buttonWithType: UIButtonTypeRoundedRect]; [button setTitle: @ "change" forState: UIControlStateNormal]; button. frame = CGRectMake (10, 10, 60, 40); [button addTarget: self action: @ selector (changeU IView) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: button];}-(void) changeUIView {[UIView beginAnimations: @ "animation" context: nil]; [UIView setAnimationDuration: 1.0f]; [UIView setAnimationCurve: birthday]; [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView: self. view cache: YES]; [UIView commitAnimations];} The following figure shows the effect after clicking change (two types ): There are four types of animation constants: [cpp] UIViewAnimationTransitionNone, assign, assign, UIViewAnimationTransitionCurlUp, UIViewAnimationTransitionCurlDown, and 1.2 switch the two view positions in the current view controller [self. view exchangeSubviewAtIndex: 1 withSubviewAtIndex: 0]; add two views, one r view plaincopy-(void) viewDidLoad {[super viewDidLoad]; UIView * redView = [UIView alloc] initWithFrame: [[UISc Reen mainScreen] bounds]; redView. backgroundColor = [UIColor redColor]; [self. view addSubview: redView]; UIView * yellowView = [[UIView alloc] initWithFrame: [UIScreen mainScreen] bounds]; yellowView. backgroundColor = [UIColor yellowColor]; [self. view addSubview: yellowView]; UIButton * button = [UIButton buttonWithType: UIButtonTypeRoundedRect]; [button setTitle: @ "change" forState: UIControlStateNormal]; Button. frame = CGRectMake (10, 10,300, 40); [button addTarget: self action: @ selector (changeUIView) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: button]; UIButton * button1 = [UIButton buttonWithType: callback]; [button1 setTitle: @ "Change 1" forState: UIControlStateNormal]; button1.frame = CGRectMake (10, 60,300, 40); [button1 addTarget: self action: @ selector (changeUIV Iew1) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: button1];} [cpp]-(void) changeUIView1 {[UIView progress: @ "animation" context: nil]; [UIView setAnimationDuration: 1.0f]; [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut]; [UIView setAnimationTransition: UIViewAnimationTransitionCurlDown forView: self. view cache: YES]; // two view locations in the current view controller [self. view exchangeSubv IewAtIndex: 1 withSubviewAtIndex: 0]; [UIView commitAnimations];} it looks like two pages. 1.3. [UIView setAnimationDidStopSelector: @ selector (animationFinish :)]; you can set the callback after the animation is completed before the commitAnimations message. The setting method is [UIView setAnimationDidStopSelector: @ selector (animationFinish :)]; 2. Use: CATransition [cpp]-(void) changeUIView2 {CATransition * transition = [CATransition animation]; transition. duration = 2.0f; transition. type = kCATransitionPush; transition. subtype = kCATransitionFromTop; [self. view exch AngeSubviewAtIndex: 1 withSubviewAtIndex: 0]; [self. view. layer addAnimation: transition forKey: @ "animation"];} transition. the type can be diluted, pushed, uncovered, and overwritten by NSString * const kCATransitionFade; NSString * const pushed; NSString * const kCATransitionPush; NSString * const kCATransitionReveal; these four types are transition. subtype also has four types of NSString * const kCATransitionFromRight; NSString * const kCATransitionFromLeft; NSString * Const kCATransitionFromTop; NSString * const kCATransitionFromBottom; 2.2 proprietary animation types: Cube, absorption, flip, ripple, flip, lens on, and lens off [cpp] animation. type = @ "cube" animation. type = @ "suckEffect"; animation. type = @ "oglFlip"; // no matter whether subType is "fromLeft" or "fromRight", official has only one animation effect. type = @ "rippleEffect"; animation. type = @ "pageCurl"; animation. type = @ "pageUnCurl" animation. type = @ "cameraIrisHollowOpen"; Nimation. type = @ "cameraIrisHollowClose"; is the effect of the first cube: 2.3 CATransition's startProgress endProgress attribute. These two attributes are float type. You can control the animation process and place the animation on a certain animation point. The value ranges from 0.0 to 1.0. EndProgress must be greater than or equal to startProgress. For example, if the above cube is switched to, you can set endProgress = 0.5 to let the animation stay in the normal rotation position. The above private animation effects should be used with caution in practical applications. This is because the app store may reject these animation effects during review. 3. + (void) animateWithDuration :( NSTimeInterval) duration animations :( void (^) (void) animations completion :( void (^) (BOOL finished) completion Method of UIView. This method is supported after iOS4.0. It is simpler and easier to use than the UIView method in 1. Add moveView to DidView. [Cpp] moveView = [[UIView alloc] initWithFrame: CGRectMake (10,180,200, 40)]; moveView. backgroundColor = [UIColor blackColor]; [self. view addSubview: moveView]; [cpp]-(void) changeUIView3 {[UIView animateWithDuration: 3 animations: ^ (void) {moveView. frame = CGRectMake (10,270,200, 40);} completion: ^ (BOOL finished) {UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (20, 20, 40, 40)]; label. ba CkgroundColor = [UIColor blackColor]; [self. view addSubview: label];} Then use the UIView animateWithDuration animation to move the animation. After the animation is moved, add a Label. 3.2 use [cpp]-(void) changeUIView3 {[UIView animateWithDuration: 2 delay: 0 options: Using animations: ^ (void) {moveView. alpha = 0.0;} completion: ^ (BOOL finished) {[UIView animateWithDuration: 1 delay: 1.0 options: available | refreshing animations: ^ (void) {[UIView setAnimationRepeatCount: 2.5]; moveView. alpha = 1.0;} Completion: ^ (BOOL finished) {}];} the nested effect is to first change the view to transparent, from transparent to opaque, repeat the results of transparent to opacity for 2.5 times.

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.