• 设计思路:
  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. Unity 中场景切换

    Unity游戏开发中,单个Scene解决所有问题似乎不可能,那么多个Scene之间的切换是必然存在.如果仅仅是切换,似乎什么都好说,但是在场景比较大的时候不想让玩家等待加载或者说场景与场景之间想通过一 ...

  2. 不再写.bat

    <script type="text/javascript"> for (var w = 0; w < 24; w++) { setTimeout(functio ...

  3. Technical analysis of client identification mechanisms

    http://www.chromium.org/Home/chromium-security/client-identification-mechanisms Chromium‎ > ‎Chro ...

  4. 统计学习方法笔记 -- Boosting方法

    AdaBoost算法 基本思想是,对于一个复杂的问题,单独用一个分类算法判断比较困难,那么我们就用一组分类器来进行综合判断,得到结果,"三个臭皮匠顶一个诸葛亮" 专业的说法, 强可 ...

  5. Windows注册表(持续更新)

    HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Zoom 下, 设置DWORD 值 ZoomDisabled  等于 1.

  6. CRUD操作 create创建 read读取 update修改 delete删除

    1.注释语法:--,#2.后缀是.sql的文件是数据库查询文件3.保存查询4.在数据库里面 列有个名字叫字段 行有个名字叫记录 CRUD操作:create 创建(添加)read 读取update 修改 ...

  7. BLE 4.0 与 4.1的区别

    蓝牙技术让我们在连接各种设备的时候不再被繁多的数据线所束缚,比如音响.电脑,甚至是汽车.目前最新的蓝牙版本是4.0,相比3.0它进一步降低了功耗,并且也提高了传输效率.近日,蓝牙技术联盟(Blueto ...

  8. 转:ASP.NET MVC + EF 更新的几种方式

    1.常用 db.Entry(实体).State = EntityState.Modified;db.SaveChanges(); 2.指定更新 db.Configuration.ValidateOnS ...

  9. [LeetCode]题解(python):100 Same Tree

    题目来源 https://leetcode.com/problems/same-tree/ Given two binary trees, write a function to check if t ...

  10. asp.net跳转页面的三种方法比较(转)

    2006-10-20 14:32 [小 大] 来源: 博客园 评论: 0分享至: 百度权重查询 词库网 网站监控 服务器监控 SEO监控 手机游戏 iPhone游戏 今天老师讲了三种跳转页面的方法,现 ...