-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathALSUIUtils.m
158 lines (149 loc) · 5.68 KB
/
ALSUIUtils.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
#import "ALSUIUtils.h"
@implementation ALSUIUtils
+(void)showError:(NSError*)error
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:STR(@"LK_SORRY")
message:error.localizedDescription delegate:nil
cancelButtonTitle:STR(@"LK_OK") otherButtonTitles:nil, nil];
[alert show];
}
+(void)showAlertWithTitle:(NSString*)title andMessage:(NSString*)message
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message delegate:nil
cancelButtonTitle:STR(@"LK_OK") otherButtonTitles:nil, nil];
[alert show];
}
+(void)customKeyboardOnSearchBar:(UISearchBar *)searchBar
{
for(UIView *subView in searchBar.subviews)
{
if([subView conformsToProtocol:@protocol(UITextInputTraits)])
{
[(UITextField *)subView setKeyboardAppearance:UIKeyboardAppearanceAlert];
[(UITextField *)subView setReturnKeyType:UIReturnKeyDone];
}
else
{
for(UIView *subSubView in [subView subviews])
{
if([subSubView conformsToProtocol:@protocol(UITextInputTraits)])
{
[(UITextField *)subSubView setReturnKeyType:UIReturnKeyDone];
[(UITextField *)subSubView setKeyboardAppearance:UIKeyboardAppearanceAlert];
}
}
}
}
}
static UIView *collectDataView = nil;
static const CGFloat xOffset = 5.0f;
static const CGFloat height = 40.0f;
static const CGFloat bounceOffset = 60.0f;
#define kMessageLabelTag 55
+(UIView*)collectDataView
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
collectDataView = [[UIView alloc] initWithFrame:CGRectMake(xOffset, -height, [[UIScreen mainScreen] bounds].size.width - xOffset*2, height)];
collectDataView.clipsToBounds = YES;
collectDataView.layer.cornerRadius = 8.0f;
collectDataView.backgroundColor = [UIColor lightGrayColor];
UILabel *countLabel = [[UILabel alloc] initWithFrame:collectDataView.bounds];
countLabel.tag = kMessageLabelTag;
countLabel.backgroundColor = [UIColor clearColor];
countLabel.font = [UIFont systemFontOfSize:14.0f];
countLabel.adjustsFontSizeToFitWidth = YES;
countLabel.textAlignment = NSTextAlignmentCenter;
countLabel.textColor = [UIColor whiteColor];
countLabel.numberOfLines = 2;
[collectDataView addSubview:countLabel];
[[self window] addSubview:collectDataView];
collectDataView.hidden = YES;
});
return collectDataView;
}
+(void)showNotificationBubbleWithMessage:(NSString*)message
{
if ([self collectDataView].hidden == NO) return;
[self collectDataView].hidden = NO;
CGRect frame = CGRectMake(xOffset, -height, [[UIScreen mainScreen] bounds].size.width - xOffset*2, height);
[self collectDataView].frame = frame;
if ([self collectDataView].superview == nil)
{
[[self window] addSubview:[self collectDataView]];
}
[[self window] bringSubviewToFront:[self collectDataView]];
[(UILabel*)[[self collectDataView] viewWithTag:kMessageLabelTag] setText:message];
__block UIView *w = [self collectDataView];
__block typeof(self) weakSelf = self;
[UIView animateWithDuration:0.4 animations:^{
CGRect frame = w.frame;
frame.origin.y += bounceOffset;
w.frame = frame;
w.alpha = 1.0;
} completion:^(BOOL finished) {
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[weakSelf hideMessage];
});
}];
}
+(void)hideMessage
{
__block UIView *w = [self collectDataView];
[UIView animateWithDuration:0.4 animations:^{
CGRect frame = w.frame;
frame.origin.y = 0;
w.alpha = 0;
w.frame = frame;
} completion:^(BOOL finished) {
w.hidden = YES;
}];
}
+(void)showUserHintWithMessage:(NSString*)message
{
if ([self collectDataView].hidden == NO) return;
[self collectDataView].hidden = NO;
CGRect frame = CGRectMake(xOffset, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width - xOffset*2, height);
[self collectDataView].frame = frame;
if ([self collectDataView].superview == nil)
{
[[self window] addSubview:[self collectDataView]];
}
[[self window] bringSubviewToFront:[self collectDataView]];
[(UILabel*)[[self collectDataView] viewWithTag:kMessageLabelTag] setText:message];
__block UIView *w = [self collectDataView];
__block typeof(self) weakSelf = self;
[UIView animateWithDuration:0.4 animations:^{
CGRect frame = w.frame;
frame.origin.y -= bounceOffset - 15;
w.frame = frame;
w.alpha = 1.0;
} completion:^(BOOL finished) {
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[weakSelf hideHint];
});
}];
}
+(void)hideHint
{
__block UIView *w = [self collectDataView];
[UIView animateWithDuration:0.4 animations:^{
CGRect frame = w.frame;
frame.origin.y = [[UIScreen mainScreen] bounds].size.height;
w.alpha = 0;
w.frame = frame;
} completion:^(BOOL finished) {
w.hidden = YES;
}];
}
+(UIWindow*)window
{
id <UIApplicationDelegate> app = [[UIApplication sharedApplication] delegate];
return app.window;
}
@end