进球弹出提醒效果
最近研究UIView动画,对比分在线客户端使用了很多UIView动画效果。

一直以来都是使用UIView animateWithDuration实现UIView动画,但是这种模式无法实现连续动,每个UIView只能同步播放,由于刚接触iOS开发,一直找不到解决方法,网上找了许多都是使用CAKeyframeAnimation来实现,虽然CAKeyframeAnimation网上实现起来更简单,但我觉得还是有点麻烦。于是自己扩展了UIView实现连续动画,就是一段动画运行完毕后调用另一段动画,保证两段动画没有重叠。
大概有两种方法可以选择:
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
以后这样调用就可以了:
- -(void) displayAnimate{
- self.alpha = 1;
- CGRect _frame = self.frame;
- _frame.origin.x = -100;
- self.frame = _frame;
- [UIView beginAnimations:nil context:nil];
- [UIView setAnimationDuration:0.15];
- _frame.origin.x = x+100;
- self.frame = _frame;
- self.alpha = 0;
- [UIView commitModalAnimations];
- [UIView beginAnimations:nil context:nil];
- [UIView setAnimationDuration:0.15];
- _frame.origin.x = x;
- self.frame = _frame;
- self.alpha = 1;
- [UIView commitModalAnimations];
- }

浙公网安备 33010602011771号