ios-完成任务状态栏弹出提示view的小框架设计
- 设计思路:
- 创建单例,当设置提示view的属性时,可以随时访问到,并且只有一份.
- 创建对应的类方法.提供设置提示内容content,提示内容对应的图片image,提示view背景色以及背景图片的设置(满足更多人的要求)
- 创建类方法:设置提示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的小框架设计的更多相关文章
- iOS bug 之 H5 页面没有弹出提示框
描述:在安卓上有提示框,但是在iOS上没有提示框. step 1: 失误,是我没有在正确的位置设置网址. step 2: 修改之后,测试页能弹出提示框,但是正式的页面没有提示框. step 3: 我输 ...
- android标题栏上面弹出提示框(二) PopupWindow实现,带动画效果
需求:上次用TextView写了一个从标题栏下面弹出的提示框.android标题栏下面弹出提示框(一) TextView实现,带动画效果, 总在找事情做的产品经理又提出了奇葩的需求.之前在通知栏显示 ...
- android标题栏下面弹出提示框(一) TextView实现,带动画效果
产品经理用的是ios手机,于是android就走上了模仿的道路.做这个东西也走了一些弯路,写一篇博客放在这里,以后自己也可用参考,也方便别人学习. 弯路: 1.刚开始本来用PopupWindow去实现 ...
- SilverLight 页面后台方法XX.xaml.cs 创建JS,调用JS ,弹出提示框
1.Invoke和InvokeSelf [c-sharp] view plaincopy public partial class CreateJSDemo : UserControl { publi ...
- IOS项目之弹出动画终结篇
在之前写过IOS项目之弹出动画一.IOS项目之弹出动画二.IOS项目之弹出动画三,今天来一个终极封装已经上传到Github上弹出动画总结篇UIPopoverTableView. UIPopoverTa ...
- IOS项目之弹出动画二
在IOS项目之弹出动画一中只是实现也功能,并没有体现面向对象的思想 ,今天就试着把它封装了一下,弹出视图的内容可以根据自定义,此处只是用UIDatePicker来演示 我把它传到了GitHub上 ...
- EF 在controller弹出提示消息
第一种方式: return Content("<script>alert('此名称课程再次班级中已经存在!');window.location.href = 'Course/Cr ...
- angularJS配合bootstrap动态加载弹出提示内容
1.bootstrp的弹出提示 bootstrap已经帮我们封装了非常好用的弹出提示Popover. http://v3.bootcss.com/javascript/#popovers 2.自定义p ...
- 如果在敲代码的时候eclipse不弹出提示,怎么办?
非常弱智的操作,我们曾经在输入System.out.println("content");的时候,当我们输入了"."之后,在输入错误,此时我们再回退至" ...
随机推荐
- Web 在线文件管理器学习笔记与总结(17)复制文件 (18)剪切文件
(17)复制文件 ① 复制文件通过copy($src,$dst) 来实现 ② 检测目标目录是否存在,如果存在则继续检测目标目录中是否存在同名文件,如果不存在则复制成功 file.func.php 中添 ...
- 挑战python
00 热身 http://www.pythonchallenge.com/pc/def/0.html import math print math.pow(2,38); # 274877906944 ...
- coursera python 学习总结
为啥要写这篇总结?早上突然想到了四个字:知行合一.实践,总结,再实践,再总结经验,积累经验,为己所用.闲话少叙,来干货: 1.目标要单一,如果想要完成课程,还要健身,还要玩玩游戏.看看电影,还学别的课 ...
- MyEclipse 使用快捷键
将光标移到文档末尾,ctrl+end将光标移到文档首,ctrl+home行首,home行尾,end.
- BLE-NRF51822教程16-BLE地址
本教程基于 sdk9+sd8.0 51822的 BLE的设备地址 可以通过如下函数函数来获得 地址的设置可以调用如下函数设置. 官方的demo工程中,都是没有主动调用过 sd_ble_gap_addr ...
- 我的第一个chrome扩展(2)——基本知识
1.manifest介绍界面:json格式 json:JavaScript Object Notation 包括两种结构: key:value对:{{"A1":"valu ...
- node.js express的安装过程
1.配置nodejs的环境变量之后执行 npm install -g express 命令: 2.如果版本为4.x需要再次执行 npm install -g express-generato ...
- JS实现一个简单的计算器
使用JS完成一个简单的计算器功能.实现2个输入框中输入整数后,点击第三个输入框能给出2个整数的加减乘除.效果如上: 第一步: 创建构建运算函数count(). 第二步: 获取两个输入框中的值和获取选择 ...
- C#连接数据库的四种方法(转)
C#连接数据库的四种方法 在进行以下连接数据库之前,请先在本地安装好Oracle Client,同时本次测试System.Data的版本为:2.0.0.0. 在安装Oracle Client上请注意, ...
- 【转】Android性能优化之布局优化篇
转自:http://blog.csdn.net/feiduclear_up/article/details/46670433 Android性能优化之布局优化篇 分类: andorid 开发2015 ...