iPhone開發(fā)創(chuàng)建連續(xù)動畫案例
iPhone開發(fā)創(chuàng)建連續(xù)動畫案例是本文要介紹的內(nèi)容,主要詳細(xì)介紹了在iphone開發(fā)中連續(xù)動畫的實(shí)現(xiàn)。來看詳細(xì)內(nèi)容。在iphone開發(fā)中,我們有些時(shí)候需要創(chuàng)建連續(xù)的動畫效果,使用戶體驗(yàn)更好。
連續(xù)動畫就是一段動畫運(yùn)行完畢后調(diào)用另一段動畫,一定要保證兩段動畫沒有重疊,本文不打算使用CAKeyframeAnimation建立連續(xù)的動畫塊,雖然使用CAKeyframeAnimation更簡單(在其他的博文中實(shí)現(xiàn)此方法)
大概有兩種方法可以選擇:
1.增加延遲以便在***段動畫結(jié)束之后在啟動第二段動畫([performSelector:withObject:afterDelay:])
2.指定動畫委托回調(diào)(animationDidStop:finished:context:)
從編程的角度來說就更容易,下面我們將擴(kuò)展類UIView的方法,通過類別引入一個新的方法----commitModalAnimations.調(diào)用此方法而不是調(diào)用commitAnimations方法,它會建立一個新的runloop,該循環(huán)只有在動畫結(jié)束的時(shí)候才會停止。
這樣確保了commitModalAnimations方法只有在動畫結(jié)束才將控制權(quán)返還給調(diào)用方法,利用此擴(kuò)展方法可以將動畫塊按順序放進(jìn)代碼中,不必做任何其他的修改就能避免動畫的重疊現(xiàn)象。
代碼如下:
- @interface UIView (ModalAnimationHelper)
- + (void) commitModalAnimations;
- @end
- @interface UIViewDelegate : NSObject
- {
- CFRunLoopRef currentLoop;
- }
- @end
- @implementation UIViewDelegate
- -(id) initWithRunLoop: (CFRunLoopRef)runLoop
- {
- if (self = [super init]) currentLoop = runLoop;
- return self;
- }
- (void) animationFinished: (id) sender
- {
- CFRunLoopStop(currentLoop);
- }
- @end
- @implementation UIView (ModalAnimationHelper)
- + (void) commitModalAnimations
- {
- CFRunLoopRef currentLoop = CFRunLoopGetCurrent();
- UIViewDelegate *uivdelegate = [[UIViewDelegate alloc] initWithRunLoop:currentLoop];
- [UIView setAnimationDelegate:uivdelegate];
- [UIView setAnimationDidStopSelector:@selector(animationFinished:)];
- [UIView commitAnimations];
- CFRunLoopRun();
- [uivdelegate release];
- }
- @end
使用:
- [self.view viewWithTag:101].alpha = 1.0f;
- // Bounce to 115% of the normal size
- [UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()];
- [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
- [UIView setAnimationDuration:0.4f];
- [self.view viewWithTag:101].transform = CGAffineTransformMakeScale(1.15f, 1.15f);
- [UIView commitModalAnimations];
- // Return back to 100%
- [UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()];
- [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
- [UIView setAnimationDuration:0.3f];
- [self.view viewWithTag:101].transform = CGAffineTransformMakeScale(1.0f, 1.0f);
- [UIView commitModalAnimations];
- // Pause for a second and appreciate the presentation
- [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0f]];
- // Slowly zoom back down and hide the view
- [UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()];
- [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
- [UIView setAnimationDuration:1.0f];
- [self.view viewWithTag:101].transform = CGAffineTransformMakeScale(0.01f, 0.01f);
- [UIView commitModalAnimations];
小結(jié):iPhone開發(fā)創(chuàng)建連續(xù)動畫案例的內(nèi)容介紹完了,希望本文對你有所幫助!