iPhone开发创建连续动画案例是本文要介绍的内容,主要详细介绍了在iphone开发中连续动画的实现。来看详细内容。在iphone开发中,我们有些时候需要创建连续的动画效果,使用户体验更好。
连续动画就是一段动画运行完毕后调用另一段动画,一定要保证两段动画没有重叠,本文不打算使用CAKeyframeAnimation建立连续的动画块,虽然使用CAKeyframeAnimation更简单(在其他的博文中实现此方法)
大概有两种方法可以选择:
1.增加延迟以便在***段动画结束之后在启动第二段动画([performSelector:withObject:afterDelay:])
2.指定动画委托回调(animationDidStop:finished:context:)
从编程的角度来说就更容易,下面我们将扩展类UIView的方法,通过类别引入一个新的方法----commitModalAnimations.调用此方法而不是调用commitAnimations方法,它会建立一个新的runloop,该循环只有在动画结束的时候才会停止。
这样确保了commitModalAnimations方法只有在动画结束才将控制权返还给调用方法,利用此扩展方法可以将动画块按顺序放进代码中,不必做任何其他的修改就能避免动画的重叠现象。
代码如下:
- @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];
小结:iPhone开发创建连续动画案例的内容介绍完了,希望本文对你有所帮助!