不管用同步请求,还是异步请求如果网络不佳总会出现几秒钟的请求时间!!
在同步请求中:表现为界面所有UI不在接收用户的操作响应(程序假死状态!)
在异步请求中:表现为点击程序没有任何反应,过几秒钟后请求后数据。更新UI!(程序反应迟钝现象)
为了让用户体验度好一点,就有了在异步请求的过程中 做一个请求数据期间的动画,让用户知道当前处于数据请求阶段。
下面贴一下代码
// // SearchAnimationView.h // 查询数据时动画 // // Created by admin on 16/1/19. // Copyright © 2016年 123. All rights reserved. // #import <UIKit/UIKit.h> @interface SearchAnimationView : UIView +(void)showInSuperView:(UIView *)superView; +(void)hiddenSuperView:(UIView *)superView; @end
// // SearchAnimationView.m // 查询数据时动画 // // Created by admin on 16/1/19. // Copyright © 2016年 123. All rights reserved. // #import "SearchAnimationView.h" @implementation SearchAnimationView +(void)showInSuperView:(UIView *)superView { SearchAnimationView *sa = [superView viewWithTag:42310]; if (!sa) { sa = [[SearchAnimationView alloc]initWithFrame:CGRectMake(0, 0, 200, 100)]; sa.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"notify2"]]; sa.tag = 42310; sa.center = superView.center; sa.layer.cornerRadius = 12.f; [superView addSubview:sa]; UIButton *background = [[UIButton alloc]initWithFrame:superView.frame]; background.backgroundColor = [UIColor clearColor]; background.tag = 638425; [background addTarget:sa action:@selector(doNothing) forControlEvents:UIControlEventTouchUpInside]; [superView addSubview:background]; NSMutableArray *arr = [NSMutableArray arrayWithCapacity:36]; for (int i = 1; i < 37; i++) { UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"1_%d",i]]; [arr addObject:image]; } UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 42, 42)]; imageView.tag = 76952; imageView.center = CGPointMake(sa.bounds.size.width/2, sa.bounds.size.height/2); imageView.animationImages = arr; imageView.animationRepeatCount = 0; imageView.animationDuration = 0.75; [imageView startAnimating]; [sa addSubview:imageView]; } [UIView animateWithDuration:0.5 animations:^{ sa.hidden = NO; UIImageView *imageView = [sa viewWithTag:76952]; UIButton *background = [sa viewWithTag:638425]; if (![imageView isAnimating]) { [imageView startAnimating]; } background.hidden = NO; [superView bringSubviewToFront:background]; [superView bringSubviewToFront:sa]; }]; } +(void)hiddenSuperView:(UIView *)superView { SearchAnimationView *sa = [superView viewWithTag:42310]; if (sa) { [UIView animateWithDuration:0.5 animations:^{ UIImageView *imageView = (UIImageView *)[sa viewWithTag:76952]; [imageView stopAnimating]; sa.hidden = YES; [superView sendSubviewToBack:sa]; UIButton *background = (UIButton *)[superView viewWithTag:638425]; background.hidden = YES; [superView sendSubviewToBack:background]; }]; } } -(void)doNothing { NSLog(@"***** 点击在透明的遮罩上 *****"); } @end
/* 使用遮罩层的作用:是为了防止在数据请求期间用户点击了其他的操作干扰UI,或者就不想让用户在请求数据期间点到UI界面的其他控件*/
调用方式:
在开始请求数据的时候:
- (IBAction)startAnimation:(UIButton *)sender { //多用于正在请求数据的时候--开始请求动画 [SearchAnimationView showInSuperView:self.view];
//用于测试隐藏函数----多用于请求数据成功 [self performSelector:@selector(targetMethod) withObject:nil afterDelay:3.0]; }
在请求数据成功之后:
-(void)targetMethod { //关闭请求动画 [SearchAnimationView hiddenSuperView:self.view]; }
文章到这已经结束啦,希望对路过的朋友用所帮助!!!