• 设计思路:
  1. 创建单例,当设置提示view的属性时,可以随时访问到,并且只有一份.
  2. 创建对应的类方法.提供设置提示内容content,提示内容对应的图片image,提示view背景色以及背景图片的设置(满足更多人的要求)
  3. 创建类方法:设置提示view弹出的动画时间,以及弹出后持续显示的时间.等等
  • 下面上源代码.h文件:
  • 这里提供了两个设置提示框view的内容与内容对应图片的方法,第二个方法给出了更多的选择.多出了可以设置背景色,以及背景图片.
  • 注意点: 不建议同时设置提示view的背景色以及背景图片.背景图片的高度20最好,宽度要在375以上.

//
// PBStatusBarHUD.h
// PBTestDemo
//
// Created by 裴波波 on 16/3/31.
// Copyright © 2016年 裴波波. All rights reserved.
// #import <UIKit/UIKit.h> @interface PBStatusBarHUD : NSObject /**
* 设置显示内容及对应图片
*
* @param message 显示内容
* @param image 显示图
*/
+(void)showMessageWithString:(NSString *)message andImage:(UIImage *)image; /**
* 显示内容,内容对应的图片.设置背景色,背景图片
*
* @param message 显示内容
* @param image 文字对应图片
* @param color 背景色
* @param img 背景图
* 不建议背景色和背景图同时设置
*/
+(void)showMessage:(NSString *)message img:(UIImage *)img backgroundColor:(UIColor *)backgroundColor backgroundImg:(UIImage *)backgroundImg; /**
* 设置动画时间,以及停留显示的时间
*
* @param duration 动画执行时间
* @param continuedTime 持续显示时间
*/
+(void)setShowDurationTime:(NSTimeInterval)duration andContinuedTime:(NSTimeInterval)continuedTime; /**
* 创建单例设置动画时间 字体大小
*/
+(instancetype)sharedPbHud; /**
* 设置显示内容与对应图片的距离
*/
+(void)setDistanceFromTextToImage:(CGFloat) distance; /**
* 设置显示内容字体大小
*
* @param contentSize 字体大小CGFloat类型
*/
+(void)setContentSize:(CGFloat) contentSize; @end
  • ** .m文件**

//
// PBStatusBarHUD.m
// PBTestDemo
//
// Created by 裴波波 on 16/3/31.
// Copyright © 2016年 裴波波. All rights reserved.
// #import "PBStatusBarHUD.h" UIWindow * _window;
//屏幕宽度
#define bbScreenWidth [UIScreen mainScreen].bounds.size.width
//默认动画执行以及停留时间
#define bbDuration 1.0 @interface PBStatusBarHUD () //动画执行时间---弹出提示的view用得时间
@property(nonatomic,assign) NSTimeInterval durationTime;
//弹出提示的view后停留在屏幕上的时间
@property(nonatomic,assign) NSTimeInterval continuedTime;
//字体的大小
@property(nonatomic,assign) int contentSize;
//内容与内容对应图片的距离,建议10-20之间
@property(nonatomic,assign) CGFloat distance;
@end
  • 对屏幕宽度宏定义方便方法中访问.
  • 上面的全局变量都是为了让用户设置对应的属性后,保存下来
  • 保存的位置上面已经说过了就是利用单例的特性来保存.否则很难实现设置属性的功能.

@implementation PBStatusBarHUD //设置默认显示字体大小
-(int)contentSize{ if (_contentSize == 0) {
_contentSize = 12;
}
return _contentSize;
} //初始化内容与图片距离
-(CGFloat)distance{ if (_distance == 0) {
_distance = 10;
}
return _distance;
} /**
* 初始化动画执行以及停留时间
*/
-(NSTimeInterval)durationTime{ if (_durationTime == 0) {
_durationTime = bbDuration;
}
return _durationTime;
} -(NSTimeInterval)continuedTime{ if (_continuedTime == 0) {
_continuedTime = bbDuration;
}
return _continuedTime;
}
  • 对全局变量都进行初始化,如果没有对要显示的字体大小,动画时间,等等 要求不高的,就设置为默认的即可.方法调用的时候对象传递nil即可,基本数据类型传递0即可.

/**
* 设置显示内容及背景图
*
* @param message 显示内容
* @param image 背景图
*/
+(void)showMessageWithString:(NSString *)message andImage:(UIImage *)image{ PBStatusBarHUD * hud = [PBStatusBarHUD sharedPbHud];
//判断是否当前已经有显示,如有弹出的view正在显示,则返回.
if(_window) return;
//状态条有文字有图片button比较合适
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
//显示字体大小
button.titleLabel.font = [UIFont systemFontOfSize:hud.contentSize];
//内容左边与图片间距+10
button.titleEdgeInsets = UIEdgeInsetsMake(0, hud.distance, 0, 0);
[button setTitle:message forState:UIControlStateNormal];
[button setImage:image forState:UIControlStateNormal];
_window = [[UIWindow alloc] init];
//默认的背景颜色黑色
_window.backgroundColor = [UIColor blackColor];
_window.windowLevel = UIWindowLevelAlert;
_window.frame = CGRectMake(0, -20, bbScreenWidth, 20);
button.frame = _window.bounds;
[_window addSubview:button];
//隐藏window设置为no
_window.hidden = NO;
//设置动画
[UIView animateWithDuration:hud.durationTime animations:^{
CGRect frame = _window.frame;
frame.origin.y = 0;
_window.frame = frame;
} completion:^(BOOL finished) {
[UIView animateKeyframesWithDuration:hud.durationTime delay:hud.continuedTime options:kNilOptions animations:^{
CGRect frame = _window.frame;
frame.origin.y = -20;
_window.frame = frame;
} completion:^(BOOL finished) {
_window = nil;
}];
}];
}
  • 动画执行时间,显示提示view的时间,字体大小等等都是利用单例来实现赋值的.所以单例作用巨大.

//设置背景图,背景色,显示内容,内容对应图片
+(void)showMessage:(NSString *)message img:(UIImage *)img backgroundColor:(UIColor *)backgroundColor backgroundImg:(UIImage *)backgroundImg{ PBStatusBarHUD * hud = [PBStatusBarHUD sharedPbHud];
//判断是否当前已经有显示
if(_window) return;
//状态条有文字有图片button比较合适
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
//显示字体大小
button.titleLabel.font = [UIFont systemFontOfSize:hud.contentSize];
//左边与图片间距+10
button.titleEdgeInsets = UIEdgeInsetsMake(0, hud.distance, 0, 0);
[button setTitle:message forState:UIControlStateNormal];
[button setImage:img forState:UIControlStateNormal];
_window = [[UIWindow alloc] init];
//设置button背景图片,如果传递进来则设置上背景图
if (backgroundImg) {
[button setBackgroundImage:backgroundImg forState:UIControlStateNormal];
}
_window.backgroundColor = [UIColor blackColor];
//如果设置了背景色,显示背景色.
if (backgroundColor) {
_window.backgroundColor = backgroundColor;
}
_window.windowLevel = UIWindowLevelAlert;
_window.frame = CGRectMake(0, -20, bbScreenWidth, 20);
button.frame = _window.bounds;
[_window addSubview:button];
//隐藏window设置为no
_window.hidden = NO;
//设置动画
[UIView animateWithDuration:hud.durationTime animations:^{
CGRect frame = _window.frame;
frame.origin.y = 0;
_window.frame = frame;
} completion:^(BOOL finished) {
[UIView animateKeyframesWithDuration:hud.durationTime delay:hud.continuedTime options:kNilOptions animations:^{
CGRect frame = _window.frame;
frame.origin.y = -20;
_window.frame = frame;
} completion:^(BOOL finished) {
_window = nil;
}];
}];
}
  • 与上一个方法不同的是.多了设置背景图,背景颜色.


//设置动画执行时间,停留时间
+(void)setShowDurationTime:(NSTimeInterval)duration andContinuedTime:(NSTimeInterval)continuedTime{ PBStatusBarHUD * hud = [PBStatusBarHUD sharedPbHud];
hud.durationTime = duration;
hud.continuedTime = continuedTime;
}
  • 对外提供接口,设置动画时间.

//创建单例
+(instancetype)sharedPbHud{ static id instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [self new];
});
return instance;
} //图片与内容距离
+(void)setDistanceFromTextToImage:(CGFloat) distance{ PBStatusBarHUD * hud = [PBStatusBarHUD sharedPbHud];
hud.distance = distance;
}
  • 默认我设置的图片与文字有10的间距,自己可以通过上面这个方法进行设定间距.不建议设置太大的间距

//设置显示的字体大小
+(void)setContentSize:(CGFloat) contentSize{ PBStatusBarHUD * hud = [PBStatusBarHUD sharedPbHud];
hud.contentSize = contentSize;
} @end
  • 还可以提供更多的接口:例如设置显示文字的颜色,设置颜色的富文本,也就是文字的属性等等...

ios-完成任务状态栏弹出提示view的小框架设计的更多相关文章

  1. iOS bug 之 H5 页面没有弹出提示框

    描述:在安卓上有提示框,但是在iOS上没有提示框. step 1: 失误,是我没有在正确的位置设置网址. step 2: 修改之后,测试页能弹出提示框,但是正式的页面没有提示框. step 3: 我输 ...

  2. android标题栏上面弹出提示框(二) PopupWindow实现,带动画效果

    需求:上次用TextView写了一个从标题栏下面弹出的提示框.android标题栏下面弹出提示框(一) TextView实现,带动画效果,  总在找事情做的产品经理又提出了奇葩的需求.之前在通知栏显示 ...

  3. android标题栏下面弹出提示框(一) TextView实现,带动画效果

    产品经理用的是ios手机,于是android就走上了模仿的道路.做这个东西也走了一些弯路,写一篇博客放在这里,以后自己也可用参考,也方便别人学习. 弯路: 1.刚开始本来用PopupWindow去实现 ...

  4. SilverLight 页面后台方法XX.xaml.cs 创建JS,调用JS ,弹出提示框

    1.Invoke和InvokeSelf [c-sharp] view plaincopy public partial class CreateJSDemo : UserControl { publi ...

  5. IOS项目之弹出动画终结篇

    在之前写过IOS项目之弹出动画一.IOS项目之弹出动画二.IOS项目之弹出动画三,今天来一个终极封装已经上传到Github上弹出动画总结篇UIPopoverTableView. UIPopoverTa ...

  6. IOS项目之弹出动画二

    在IOS项目之弹出动画一中只是实现也功能,并没有体现面向对象的思想 ,今天就试着把它封装了一下,弹出视图的内容可以根据自定义,此处只是用UIDatePicker来演示 我把它传到了GitHub上    ...

  7. EF 在controller弹出提示消息

    第一种方式: return Content("<script>alert('此名称课程再次班级中已经存在!');window.location.href = 'Course/Cr ...

  8. angularJS配合bootstrap动态加载弹出提示内容

    1.bootstrp的弹出提示 bootstrap已经帮我们封装了非常好用的弹出提示Popover. http://v3.bootcss.com/javascript/#popovers 2.自定义p ...

  9. 如果在敲代码的时候eclipse不弹出提示,怎么办?

    非常弱智的操作,我们曾经在输入System.out.println("content");的时候,当我们输入了"."之后,在输入错误,此时我们再回退至" ...

随机推荐

  1. HRBUST 1326 循环找父节点神术

    题意 给出一个图 给出a点到每个点的路径 最后经过的除这个点本身以外的点 现在把a点改为b点 让求出按上面那种方式 把除b之外的点对应的点列出 ...算了我描述题意得能力好差...这个锅还是给出题的吧 ...

  2. Myeclipse 多版本共存

    Myeclipse2015 stable2.0 和Myeclipse10 共存的解决办法: 1. 准备内容: 1) myeclipse-2015-stable-2.0-offline-installe ...

  3. python函数参数

    1.位置参数 2.默认参数 指向参数为不可变对象 3.可变参数 **args    一个列表list或是元组tuple 4.关键字参数 **kw,是一个字典dict 5.命名关键字参数 *,

  4. Python浮点数控制小数精度

    将精度高的浮点数转换成精度低的浮点数. 1.round()内置方法 这个是使用最多的,刚看了round()的使用解释,也不是很容易懂.round()不是简单的四舍五入的处理方式. For the bu ...

  5. [转]LaTeX处女级入门命令语法集

    1.LaTeX文件的框架如下: \documentclass{article} \begin{document} This is the body of the article \end{docume ...

  6. CSS布局总结

    三种布局模型: 1.flow 标准流布局 2.float 浮动布局 3.layer 层叠布局 关于(flow) 标准流布局 浏览器默认的布局方式就是标准流布局.对于标准流布局下的的块元素和行内元素的特 ...

  7. Delphi如何打开DBF数据库

    Delphi语言,无论Delphi7.Delphi2007或者Delphi XE2或3,无需安装其它附加的部件,就可以实现DBF文件的打开及相关操作,网络上很多要用到什么ADO引擎的,其实未必,只有安 ...

  8. random circle

    <!doctype html><meta charset="utf-8"><html><head><title>D3 t ...

  9. 【iTerm2】美化你的Terminal 赠佛祖像

    我们开发就是喜欢各种酷炫的东西,对于有洁癖的我,连命令行都不放过了 先上图看效果,命令行显示高亮部分 实现过程: 第一步:.bash_prompt脚本 # ~/.bash_prompt # This  ...

  10. ssi服务器端指令

    SSI使用详解 你是否曾经或正在为如何能够在最短的时间内完成对一个包含上千个页面的网站的修改而苦恼?那么可以看一下本文的介绍,或许能够对你有所帮助.什么是SSI?SSI是英文Server Side I ...