简单封装新浪提示框

演示图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//  HJStatusBarHUD.h
//
// Created by luo.h on 16/3/11.
// Copyright © 2016年 appledev. All rights reserved.
//
#import <UIKit/UIKit.h>

@interface HJStatusBarHUD : UIView

@property (nonatomic,copy) NSString * text;
@property (nonatomic,strong) UILabel *textLabel;

//类方法创建HUD
+(instancetype)statusBarHUD;

//显示提示文本信息
+(void)showTips:(UIViewController *)controller withText:(NSString *)text;
+(void)showHUB:(HJStatusBarHUD *)StatusBarHUD Duration:(NSTimeInterval)duration;

@end

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
// HJStatusBarHUD.m
//
// Created by luo.h on 16/3/11.
// Copyright © 2016年 appledev. All rights reserved.
//

#import "HJStatusBarHUD.h"

#define DEFAULT_HEIGHT 44.0f

@implementation HJStatusBarHUD

- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Set default values for properties
self.contentMode = UIViewContentModeCenter;
self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin
| UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

// Transparent background
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
[self addSubview:self.textLabel];
}
return self;
}


- (NSString *)text
{
return _textLabel.text;
}

- (void)setText:(NSString *)text
{
_textLabel.text = text;
}


- (UILabel *)textLabel {
if(_textLabel == nil) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width,self.bounds.size.height)];
label.textAlignment = NSTextAlignmentCenter;
label.adjustsFontSizeToFitWidth = YES;
label.textColor = [UIColor whiteColor];
_textLabel =label;
}
return _textLabel;
}


- (void)dealloc
{
_textLabel=nil;
}


+(instancetype)statusBarHUD
{
return [[self alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, DEFAULT_HEIGHT)];
}



+(void)showTips:(UIViewController *)controller withText:(NSString *)text Duration:(NSTimeInterval)duration;
{
CGRect frame=CGRectMake(0, 0, controller.view.bounds.size.width,DEFAULT_HEIGHT);
HJStatusBarHUD * HUD=[HJStatusBarHUD statusBarHUD];
if([controller.parentViewController isKindOfClass:[UINavigationController class]] && !controller.navigationController.navigationBar.isHidden){
frame.origin.y = controller.navigationController.navigationBar.height+20-DEFAULT_HEIGHT;
HUD.frame=frame;
// 5.添加到导航控制器的view
[controller.navigationController.view insertSubview:HUD belowSubview:controller.navigationController.navigationBar];
}else{
frame.origin.y = 20-DEFAULT_HEIGHT;
HUD.frame=frame;
[controller.view addSubview:HUD];
}
HUD.text = text;
HUD.backgroundColor=[UIColor orangeColor];
[HJStatusBarHUD showHUB:HUD Duration:duration];//显示提示框
}




+(void)showTips:(UIViewController *)controller withText:(NSString *)text;
{
[self showTips:controller withText:text Duration:0.75];
}



+(void)showHUB:(HJStatusBarHUD *)StatusBarHUD Duration:(NSTimeInterval)duration
{
[UIView animateWithDuration:duration animations:^{
// 往下移动一个hub的高度
StatusBarHUD.transform = CGAffineTransformMakeTranslation(0, StatusBarHUD.height);
} completion:^(BOOL finished) { // 向下移动完毕
CGFloat delay = 1.0;
[UIView animateWithDuration:duration delay:delay options:UIViewAnimationOptionCurveEaseInOut animations:^{
// 恢复到原来的位置
StatusBarHUD.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
// 删除控件
[StatusBarHUD removeFromSuperview];
}];
}];

}
@end

文章作者: kyren
文章链接: http://huluo666.github.io/2016/03/23/简单封装新浪提示框/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Kyren's Blog