forked from shaojiankui/JKCategories
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIViewController+TopBarMessage.m
288 lines (231 loc) · 9.35 KB
/
UIViewController+TopBarMessage.m
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//
// UIViewController+TopBarMessage.m
// DXTopBarMessageView
//
// Created by xiekw on 14-3-17.
// Copyright (c) 2014年 xiekw. All rights reserved.
//
#import "UIViewController+TopBarMessage.h"
#import <objc/runtime.h>
#define kTopBarHeight 38.0f
#define kDefaultDimissDelay 3.0f
NSString * const kDXTopBarBackgroundColor = @"dx.kDXTopBarBackgroundColor";
NSString * const kDXTopBarTextColor = @"dx.kDXTopBarTextColor";
NSString * const kDXTopBarTextFont = @"dx.kDXTopBarTextFont";
NSString * const kDXTopBarIcon = @"dx.kDXTopBarIcon";
static NSMutableDictionary *__defaultTopMessageConfig = nil;
@interface TopWarningView()
@property (nonatomic, strong) NSTimer *dimissTimer;
@end
@implementation TopWarningView
- (void)dealloc
{
[self.dimissTimer invalidate];
self.dimissTimer = nil;
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
self.label.backgroundColor = [UIColor clearColor];
self.label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self addSubview:self.label];
self.iconIgv = [[UIImageView alloc] init];
[self addSubview:self.iconIgv];
#pragma mark - 轻扫手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
swipe.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
[self addGestureRecognizer:swipe];
#pragma mark - 轻击手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapNow)];
[self addGestureRecognizer:tap];
[self resetViews];
}
return self;
}
- (void)resetViews
{
if (!__defaultTopMessageConfig) {
__defaultTopMessageConfig = [@{kDXTopBarBackgroundColor : [UIColor colorWithRed:0.64 green:0.65 blue:0.66 alpha:0.96], kDXTopBarTextColor : [UIColor whiteColor], kDXTopBarTextFont : [UIFont fontWithName:@"HelveticaNeue-Medium" size:17.0]} mutableCopy];
}
self.iconIgv.image = __defaultTopMessageConfig[kDXTopBarIcon];
self.backgroundColor = __defaultTopMessageConfig[kDXTopBarBackgroundColor];
self.label.textColor = __defaultTopMessageConfig[kDXTopBarTextColor];
self.label.font = __defaultTopMessageConfig[kDXTopBarTextFont];
}
- (void)layoutSubviews
{
CGSize textSize = [self.label.text boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.bounds) * 0.9, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.label.font} context:nil].size;
CGFloat betweenIconAndText = 10.0f;
CGFloat iconWidth = 20.0f;
CGFloat labelDefaultHeight = 20.0;
if (!self.iconIgv.image) {
iconWidth = 0.0f;
}
self.iconIgv.frame = CGRectMake((CGRectGetWidth(self.bounds) - (textSize.width + iconWidth + betweenIconAndText)) * 0.5, (CGRectGetHeight(self.bounds) - iconWidth) * 0.5, iconWidth, iconWidth);
self.label.frame = CGRectMake(CGRectGetMaxX(self.iconIgv.frame) + betweenIconAndText, (CGRectGetHeight(self.bounds) - labelDefaultHeight) * 0.5, textSize.width, labelDefaultHeight);
}
- (void)setWarningText:(NSString *)warningText
{
_warningText = warningText;
self.label.text = _warningText;
[self setNeedsLayout];
}
- (void)tapNow
{
if (self.tapHandler) {
self.tapHandler();
}
}
- (void)dismiss
{
// CGRect selfFrame = self.frame;
// selfFrame.origin.y -= CGRectGetHeight(selfFrame);
//
// [UIView animateWithDuration:0.25f animations:^{
// self.frame = selfFrame;
// self.alpha = 0.3;
// } completion:^(BOOL finished) {
// [self removeFromSuperview];
// }];
//#warning mark - 视图恢复到原来位置
[UIView animateWithDuration:0.25 animations:^{
self.superview.transform = CGAffineTransformIdentity;
[self removeFromSuperview];
}];
}
#pragma mark - 从父视图上移除
- (void)willMoveToSuperview:(UIView *)newSuperview
{
[super willMoveToSuperview:newSuperview];
if (newSuperview) {
self.alpha = 1.0;
CGRect selfFrame = self.frame;
CGFloat originY = self.frame.origin.y;
selfFrame.origin.y -= CGRectGetHeight(selfFrame);
self.frame = selfFrame;
selfFrame.origin.y = originY;
[UIView animateWithDuration:0.25f animations:^{
self.frame = selfFrame;
} completion:^(BOOL finished) {
[super willMoveToSuperview:newSuperview];
}];
#pragma mark - 利用定时器来隐藏视图
[self.dimissTimer invalidate];
self.dimissTimer = nil;
//#warning mark - 修改
if (self.dimissDelay > 0) {
self.dimissTimer = [NSTimer scheduledTimerWithTimeInterval:MAX(self.dimissDelay, kDefaultDimissDelay) target:self selector:@selector(dismiss) userInfo:nil repeats:0];
}
}else {
[self.dimissTimer invalidate];
self.dimissTimer = nil;
[super willMoveToSuperview:newSuperview];
}
}
@end
///=====================================================================
static char TopWarningKey;
@implementation UIViewController (TopBarMessage)
+ (void)setTopMessageDefaultApperance:(NSDictionary *)apperance
{
if (!__defaultTopMessageConfig) {
__defaultTopMessageConfig = [NSMutableDictionary dictionary];
}
if (apperance) {
UIColor *bgColor = apperance[kDXTopBarBackgroundColor];
if (bgColor && [bgColor isKindOfClass:[UIColor class]]) {
__defaultTopMessageConfig[kDXTopBarBackgroundColor] = bgColor;
}
UIColor *textColor = apperance[kDXTopBarTextColor];
if (textColor && [textColor isKindOfClass:[UIColor class]]) {
__defaultTopMessageConfig[kDXTopBarTextColor] = textColor;
}
UIImage *icon = apperance[kDXTopBarIcon];
if (icon && [icon isKindOfClass:[UIImage class]]) {
__defaultTopMessageConfig[kDXTopBarIcon] = icon;
}
UIFont *font = apperance[kDXTopBarTextFont];
if (font && [font isKindOfClass:[UIFont class]]) {
__defaultTopMessageConfig[kDXTopBarTextFont] = font;
}
}
}
- (void)showTopMessage:(NSString *)message
{
[self showTopMessage:message topBarConfig:nil mode:TopBarMessageModeOverlay dismissDelay:kDefaultDimissDelay withTapBlock:nil];
}
//- (void)showTopMessage:(NSString *)message topBarConfig:(NSDictionary *)config dismissDelay:(float)delay withTapBlock:(dispatch_block_t)tapHandler
- (void)showTopMessage:(NSString *)message topBarConfig:(NSDictionary *)config mode:(TopBarMessageMode)messageMode dismissDelay:(float)delay withTapBlock:(dispatch_block_t)tapHandler
{
TopWarningView *topV = objc_getAssociatedObject(self, &TopWarningKey);
if (!topV) {
topV = [[TopWarningView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), kTopBarHeight)];
objc_setAssociatedObject(self, &TopWarningKey, topV, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
CGFloat startY = 0.0;
if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
switch (self.edgesForExtendedLayout) {
case UIRectEdgeTop:
case UIRectEdgeAll:
startY = 64.0;
break;
case UIRectEdgeBottom:
case UIRectEdgeLeft:
case UIRectEdgeNone:
case UIRectEdgeRight:
startY = 0.0;
break;
default:
break;
}
}
topV.frame = CGRectMake(0, startY, CGRectGetWidth(self.view.bounds), kTopBarHeight);
topV.warningText = message;
topV.tapHandler = tapHandler;
topV.dimissDelay = delay;
if (config) {
UIColor *bgColor = config[kDXTopBarBackgroundColor];
if (bgColor && [bgColor isKindOfClass:[UIColor class]]) {
topV.backgroundColor = bgColor;
}
UIColor *textColor = config[kDXTopBarTextColor];
if (textColor && [textColor isKindOfClass:[UIColor class]]) {
topV.label.textColor = textColor;
}
UIImage *icon = config[kDXTopBarIcon];
if (icon && [icon isKindOfClass:[UIImage class]]) {
topV.iconIgv.image = icon;
}
UIFont *font = config[kDXTopBarTextFont];
if (font && [font isKindOfClass:[UIFont class]]) {
topV.label.font = font;
}
}else {
[topV resetViews];
}
[self.view addSubview:topV];
//#warning mark -
if (messageMode == TopBarMessageModeResize) {
[UIView animateWithDuration:0.25f animations:^{
CGAffineTransform transform = CGAffineTransformMakeTranslation(0, kTopBarHeight);
self.view.transform = transform;
CGRect frame = topV.frame;
frame.origin.y = -kTopBarHeight;
topV.frame = frame;
}];
}
}
- (void)dismissTopMessage
{
NSLog(@"隐藏");
NSArray *subViews = self.view.subviews;
for (UIView *subView in subViews) {
if ([subView isKindOfClass:[TopWarningView class]]) {
TopWarningView *view = (TopWarningView *)subView;
[view dismiss];
}
}
}
@end