iOS在状态栏上显示提醒信息的功能定制
2017-06-09 09:38
573 查看
先看效果图
实现这个效果,用到了JDStatusBarNotification,这是一个易于使用和定制的在状态栏上显示提醒信息的控件,可自定义颜色、字体以及动画,支持进度条展示,并可以显示活动指示器。
假设这么一个场景,需要调接口修改个人资料,这时有3个状态,正在修改、修改成功、修改失败。我们可以写一个公共类,方便调用,譬如 NSObject+Common。
.h文件写方法
#import <Foundation/Foundation.h> @interface NSObject (Common) - (void)showStatusBarQueryStr:(NSString *)tipStr; - (void)showStatusBarSuccessStr:(NSString *)tipStr; //此方法在实际开发中调用,调接口失败返回的error - (void)showStatusBarError:(NSError *)error; //... - (void)showStatusBarErrorStr:(NSString *)tipStr; @end
.m文件实现方法
#import "NSObject+Common.h" #import "JDStatusBarNotification.h" @implementation NSObject (Common) //error返回的tipStr - (NSString *)tipFromError:(NSError *)error { if (error && error.userInfo) { NSMutableString *tipStr = [[NSMutableString alloc] init]; if ([error.userInfo objectForKey:@"msg"]) { NSArray *msgArray = [[error.userInfo objectForKey:@"msg"] allValues]; NSUInteger num = [msgArray count]; for (int i = 0; i < num; i++) { NSString *msgStr = [msgArray objectAtIndex:i]; if (i+1 < num) { [tipStr appendString:[NSString stringWithFormat:@"%@\n", msgStr]]; }else{ [tipStr appendString:msgStr]; } } }else{ if ([error.userInfo objectForKey:@"NSLocalizedDescription"]) { tipStr = [error.userInfo objectForKey:@"NSLocalizedDescription"]; }else{ [tipStr appendFormat:@"ErrorCode%ld", (long)error.code]; } } return tipStr; } return nil; } - (void)showStatusBarQueryStr:(NSString *)tipStr { [JDStatusBarNotification showWithStatus:tipStr styleName:JDStatusBarStyleSuccess]; [JDStatusBarNotification showActivityIndicator:YES indicatorStyle:UIActivityIndicatorViewStyleWhite]; } - (void)showStatusBarSuccessStr:(NSString *)tipStr { if ([JDStatusBarNotification isVisible]) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [JDStatusBarNotification showActivityIndicator:NO indicatorStyle:UIActivityIndicatorViewStyleWhite]; [JDStatusBarNotification showWithStatus:tipStr dismissAfter:1.5 styleName:JDStatusBarStyleSuccess]; }); }else{ [JDStatusBarNotification showActivityIndicator:NO indicatorStyle:UIActivityIndicatorViewStyleWhite]; [JDStatusBarNotification showWithStatus:tipStr dismissAfter:1.0 styleName:JDStatusBarStyleSuccess]; } } - (void)showStatusBarError:(NSError *)error { if ([JDStatusBarNotification isVisible]) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [JDStatusBarNotification showActivityIndicator:NO indicatorStyle:UIActivityIndicatorViewStyleWhite]; [JDStatusBarNotification showWithStatus:[self tipFromError:error] dismissAfter:1.5 styleName:JDStatusBarStyleError]; }); }else{ [JDStatusBarNotification showActivityIndicator:NO indicatorStyle:UIActivityIndicatorViewStyleWhite]; [JDStatusBarNotification showWithStatus:[self tipFromError:error] dismissAfter:1.5 styleName:JDStatusBarStyleError]; } } - (void)showStatusBarErrorStr:(NSString *)tipStr { if ([JDStatusBarNotification isVisible]) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [JDStatusBarNotification showActivityIndicator:NO indicatorStyle:UIActivityIndicatorViewStyleWhite]; [JDStatusBarNotification showWithStatus:tipStr dismissAfter:1.5 styleName:JDStatusBarStyleError]; }); }else{ [JDStatusBarNotification showActivityIndicator:NO indicatorStyle:UIActivityIndicatorViewStyleWhite]; [JDStatusBarNotification showWithStatus:tipStr dismissAfter:1.5 styleName:JDStatusBarStyleError]; } }
调用方法
[self showStatusBarQueryStr:@"正在修改个人信息"];
[self showStatusBarSuccessStr:@"个人信息修改成功"];
//[self showStatusBarError:error]; [self showStatusBarErrorStr:@"修改失败"];
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。
您可能感兴趣的文章:
相关文章推荐
- 做一个计时器示例 在页面上有一个文本框和一个普通按钮 在状态栏上显示,单击按钮,开始计时的提示信息 单击按钮时,在文本框显示计数的过程,同时要求按钮上的字,显示为“单击暂停” 此时单击按钮,暂停计时,同时按钮上的字显示为“继续计时”,要求这两种状态来回切换,来完成计数器的功能
- iOS状态栏获取网络类型,运营商,电池电量,显示的系统时间等信息
- Domino9下webmail功能定制--登陆信息提醒
- iOS 实现类似微博或者陌陌上的显示附近位置信息功能。
- iOS开发 - 在状态栏显示FPS,CPU和内存信息
- iOS状态栏获取网络类型,运营商,电池电量,显示的系统时间等信息
- 打卡挂机功能 显示推广帐号 等个人信息 定制需多少钱?
- 在状态栏显示信息
- 如何在ios设备上定位当前地点,并在地图上显示相关信息
- 《BREW进阶与精通——3G移动增值业务的运营、定制与开发》连载之47---BREW SDK 九大功能之显示和用户界面
- IOS中显示和隐藏状态栏的网络活动标志
- MonoDroid学习笔记(十二)—— 您有一条新短信息,请注意查收。状态栏提醒
- ios通过google map显示地图和乘车信息
- IOS锁屏状态播放音乐时显示专辑信息和图片
- How to: Display Command Information in the Status Bar在状态栏中显示命令信息
- DataGridView单元格字符超长,ToolTip冒泡提醒功能,可控制宽度和持续显示时间
- IOS 显示Debug信息
- 创建用树形列表显示文件夹目录信息的功能模块
- 使用模版列用一列显示全部信息,同时完成修改的功能
- 招聘信息显示苹果开发iOS地图应用