/
FDStatusBarNotifierView.m
217 lines (173 loc) · 7.75 KB
/
FDStatusBarNotifierView.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
//
// StatusBarNotifierView.m
// StatusBarNotifier
//
// Created by Francesco Di Lorenzo on 05/09/12.
// Copyright (c) 2012 Francesco Di Lorenzo. All rights reserved.
//
#import "FDStatusBarNotifierView.h"
NSTimeInterval const kTimeOnScreenDefault = 2.0;
@interface FDStatusBarNotifierView ()
@property (strong, nonatomic) UILabel *messageLabel;
@end
@implementation FDStatusBarNotifierView
#pragma mark - Init
- (id)init
{
self = [super init];
if (self) {
self.frame = CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 20);
self.backgroundColor = [UIColor blackColor];
self.messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, ([UIScreen mainScreen].bounds.size.width - 20), 20)];
self.messageLabel.textColor = [UIColor whiteColor];
self.messageLabel.backgroundColor = [UIColor blackColor];
self.messageLabel.textAlignment = UITextAlignmentCenter;
self.messageLabel.font = [UIFont boldSystemFontOfSize:12];
self.shouldHideOnTap = NO;
self.manuallyHide = NO;
[self addSubview:self.messageLabel];
self.timeOnScreen = kTimeOnScreenDefault;
}
return self;
}
- (id)initWithMessage:(NSString *)message
{
self = [self initWithMessage:message delegate:nil];
if (self) {
}
return self;
}
- (id)initWithMessage:(NSString *)message delegate:(id<FDStatusBarNotifierViewDelegate>)delegate
{
self = [self init];
if (self) {
self.delegate = delegate;
self.message = message;
self.messageLabel.text = message;
}
return self;
}
#pragma mark - Presentation
- (void)showInWindow:(UIWindow *)window
{
if (self.delegate && [self.delegate respondsToSelector:@selector(willPresentNotifierView:)]) {
[self.delegate willPresentNotifierView:self];
}
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
[window insertSubview:self atIndex:0];
CGFloat textWith = [self.message sizeWithFont:self.messageLabel.font
constrainedToSize:CGSizeMake(MAXFLOAT, 20)
lineBreakMode:self.messageLabel.lineBreakMode].width;
if (textWith < self.frame.size.width) { // the message to display fits in the status bar view
CGRect animationDestinationFrame;
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait) {
animationDestinationFrame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 20);
} else {
animationDestinationFrame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, 20);
}
[UIView animateWithDuration:.4
animations:^{
self.frame = animationDestinationFrame;
} completion:^(BOOL finished){
if (self.delegate && [self.delegate respondsToSelector:@selector(didPresentNotifierView:)]) {
[self.delegate didPresentNotifierView:self];
}
if (!self.manuallyHide) {
[NSTimer scheduledTimerWithTimeInterval:self.timeOnScreen
target:self
selector:@selector(hide)
userInfo:nil
repeats:NO];
}
}];
} else {
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait) {
CGRect frame = self.messageLabel.frame;
CGFloat exceed = textWith - frame.size.width;
frame.size.width = textWith;
self.messageLabel.frame = frame;
NSTimeInterval timeExceed = exceed / 60;
[UIView animateWithDuration:.4 animations:^{
self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 20);
} completion:^(BOOL finished){
if (self.delegate && [self.delegate respondsToSelector:@selector(didPresentNotifierView:)])
[self.delegate didPresentNotifierView:self];
if (!self.manuallyHide) {
[self performSelector:@selector(hide)
withObject:nil
afterDelay:self.timeOnScreen + timeExceed];
[self performSelector:@selector(doTextScrollAnimation:)
withObject:[NSNumber numberWithFloat:timeExceed]
afterDelay:self.timeOnScreen / 3];
} else {
[self performSelector:@selector(doTextScrollAnimation:)
withObject:[NSNumber numberWithFloat:timeExceed]
afterDelay:kTimeOnScreenDefault / 3];
}
}];
} else {
// add support for landscape
}
}
}
- (void)hide
{
if (self.isHidden) {
return;
}
if (self.delegate && [self.delegate respondsToSelector:@selector(willHideNotifierView:)]) {
[self.delegate willHideNotifierView:self];
}
CGRect animationDestinationFrame;
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait) {
animationDestinationFrame = CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 20);
} else {
animationDestinationFrame = CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.height, 20);
}
[UIView animateWithDuration:.4
animations:^{
self.frame = animationDestinationFrame;
[[UIApplication sharedApplication] setStatusBarHidden:NO
withAnimation:UIStatusBarAnimationSlide];
} completion:^(BOOL finished){
if (finished) {
if (self.delegate && [self.delegate respondsToSelector:@selector(didHideNotifierView:)]) {
[self.delegate didHideNotifierView:self];
}
[self removeFromSuperview];
}
}];
}
- (BOOL)isHidden
{
return (self.superview == nil);
}
- (void)doTextScrollAnimation:(NSNumber*)timeInterval
{
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait) {
__block CGRect frame = self.messageLabel.frame;
[UIView transitionWithView:self.messageLabel
duration:timeInterval.floatValue
options:UIViewAnimationCurveLinear
animations:^{
frame.origin.x = [UIScreen mainScreen].bounds.size.width - frame.size.width - frame.origin.x;
self.messageLabel.frame = frame;
} completion:nil];
} else {
// add support for landscape
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (self.shouldHideOnTap == YES) {
[self hide];
}
[self.delegate notifierViewTapped:self];
}
#pragma mark - Accessor
- (void)setMessage:(NSString *)message
{
_message = message;
self.messageLabel.text = message;
}
@end