进球弹出提醒效果

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

[img]http://dl.iteye.com/upload/attachment/529206/bc475273-2b19-3424-af9e-e7cf0dc287ac.jpg[/img]

一直以来都是使用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];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值