欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS在状态栏上显示提醒信息的功能定制

程序员文章站 2023-12-19 17:59:16
先看效果图 实现这个效果,用到了jdstatusbarnotification,这是一个易于使用和定制的在状态栏上显示提醒信息的控件,可自定义颜色、字体以及动画,...

先看效果图

iOS在状态栏上显示提醒信息的功能定制

实现这个效果,用到了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:@"修改失败"];

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。

上一篇:

下一篇: