iPhone開發建立連續動畫案例

來源:互聯網
上載者:User

iPhone開發建立連續動畫案例是本文要介紹的內容,主要詳細介紹了在iphone開發中連續動畫的實現。來看詳細內容。在iphone開發中,我們有些時候需要建立連續的動畫效果,使使用者體驗更好。

連續動畫就是一段動畫運行完畢後調用另一段動畫,一定要保證兩段動畫沒有重疊,本文不打算使用CAKeyframeAnimation建立連續的動畫塊,雖然使用CAKeyframeAnimation更簡單在其他的博文中實現此方法)

大概有兩種方法可以選擇:

1.增加延遲以便在第一段動畫結束之後在啟動第二段動畫[performSelector:withObject:afterDelay:])

2.指定動畫委託回調animationDidStop:finished:context:)

從編程的角度來說就更容易,下面我們將擴充類UIView的方法,通過類別引入一個新的方法----commitModalAnimations.調用此方法而不是調用commitAnimations方法,它會建立一個新的runloop,該迴圈只有在動畫結束的時候才會停止。

這樣確保了commitModalAnimations方法只有在動畫結束才將控制權返還給調用方法,利用此擴充方法可以將動畫塊按順序放進代碼中,不必做任何其他的修改就能避免動畫的重疊現象。

代碼如下:

 
  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 

使用:

 
  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]; 

小結:iPhone開發建立連續動畫案例的內容介紹完了,希望本文對你有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.