Case study of creating continuous animation for iPhone Development

Source: Internet
Author: User

IPhone DevelopmentContinuous CreationAnimationThe case is the content to be introduced in this article.Iphone DevelopmentMedium continuityAnimation. Let's take a look at the details. InIphone Development, We sometimes need to create continuousAnimationResults To improve user experience.

ContinuousAnimationJust a paragraphAnimationWhen calling another animation, make sure that the two animations do not overlap. In this article, we do not plan to use CAKeyframeAnimation to create continuous animation blocks, although CAKeyframeAnimation is easier to implement in other blog posts)

There are two possible methods:

1. Increase the latency so that the second animation can be started after the first animation ends [effecmselector: withObject: afterDelay:])

2. Specify the animation delegate callback animationDidStop: finished: context :)

It is easier from the programming point of view. Next we will introduce a new method-commitModalAnimations-to extend the class UIView method. call this method instead of calling the commitAnimations method. It creates a new runloop, which stops only when the animation ends.

This ensures that the commitModalAnimations method returns control to the calling method only after the animation ends. This extension method can be used to put the animation block into the Code in order, you do not need to make any other modifications to avoid overlapping animations.

The Code is as follows:

 
 
  1. @interface UIView (ModalAnimationHelper)  
  2. + (void) commitModalAnimations;  
  3. @end  
  4. @interface UIViewDelegate : NSObject  
  5. {  
  6. CFRunLoopRef currentLoop;  
  7. }  
  8. @end  
  9. @implementation UIViewDelegate  
  10. -(id) initWithRunLoop: (CFRunLoopRef)runLoop   
  11. {  
  12. if (self = [super init]) currentLoop = runLoop;  
  13. return self;  
  14. }  
  15. (void) animationFinished: (id) sender  
  16. {  
  17. CFRunLoopStop(currentLoop);  
  18. }  
  19. @end  
  20. @implementation UIView (ModalAnimationHelper)  
  21. + (void) commitModalAnimations  
  22. {  
  23. CFRunLoopRef currentLoop = CFRunLoopGetCurrent();  
  24. UIViewDelegate *uivdelegate = [[UIViewDelegate alloc] initWithRunLoop:currentLoop];  
  25. [UIView setAnimationDelegate:uivdelegate];  
  26. [UIView setAnimationDidStopSelector:@selector(animationFinished:)];  
  27. [UIView commitAnimations];  
  28. CFRunLoopRun();  
  29. [uivdelegate release];  
  30. }  
  31. @end 

Usage:

 
 
  1. [self.view viewWithTag:101].alpha = 1.0f;  
  2. // Bounce to 115% of the normal size  
  3. [UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()];  
  4. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
  5. [UIView setAnimationDuration:0.4f];  
  6. [self.view viewWithTag:101].transform = CGAffineTransformMakeScale(1.15f, 1.15f);  
  7. [UIView commitModalAnimations];  
  8. // Return back to 100%  
  9. [UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()];  
  10. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
  11. [UIView setAnimationDuration:0.3f];  
  12. [self.view viewWithTag:101].transform = CGAffineTransformMakeScale(1.0f, 1.0f);  
  13. [UIView commitModalAnimations];  
  14. // Pause for a second and appreciate the presentation  
  15. [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0f]];  
  16. // Slowly zoom back down and hide the view  
  17. [UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()];  
  18. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
  19. [UIView setAnimationDuration:1.0f];  
  20. [self.view viewWithTag:101].transform = CGAffineTransformMakeScale(0.01f, 0.01f);  
  21. [UIView commitModalAnimations]; 

Summary:IPhone onContinuous CreationAnimationI hope this article will help you with the introduction of the case!

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.