您的位置:首页 > 移动开发 > IOS开发

ios-完成任务状态栏弹出提示view的小框架设计

2016-03-31 22:19 417 查看
设计思路:

创建单例,当设置提示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


还可以提供更多的接口:例如设置显示文字的颜色,设置颜色的富文本,也就是文字的属性等等...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: