-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQZAutoScrollLabel.m
executable file
·415 lines (316 loc) · 12.3 KB
/
QZAutoScrollLabel.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//
// QZAutoScrollLabel.m
// QZAutoScrollLabel
//
#import "QZAutoScrollLabel.h"
#import <QuartzCore/QuartzCore.h>
#define kLabelCount 2
#define kDefaultFadeLength 7.f
// pixel buffer space between scrolling label
#define kDefaultLabelBufferSpace 20
#define kDefaultPixelsPerSecond 30
#define kDefaultPauseTime 1.5f
// shortcut method for NSArray iterations
static void each_object(NSArray *objects, void (^block)(id object)) {
for (id obj in objects) {
block(obj);
}
}
// shortcut to change each label attribute value
#define EACH_LABEL(ATTR, VALUE) each_object(self.labels, ^(UILabel *label) { label.ATTR = VALUE; });
@interface QZAutoScrollLabel ()
@property (nonatomic, strong) NSArray *labels;
@property (nonatomic, strong, readonly) UILabel *mainLabel;
@property (nonatomic, strong) UIScrollView *scrollView;
@end
@implementation QZAutoScrollLabel
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self commonInit];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self commonInit];
}
return self;
}
- (void)commonInit {
// create the labels
NSMutableSet *labelSet = [[NSMutableSet alloc] initWithCapacity:kLabelCount];
for (int index = 0; index < kLabelCount; ++index) {
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.autoresizingMask = self.autoresizingMask;
// store labels
[self.scrollView addSubview:label];
[labelSet addObject:label];
}
self.labels = [labelSet.allObjects copy];
// default values
_scrollDirection = QZAutoScrollDirectionLeft;
_scrollSpeed = kDefaultPixelsPerSecond;
self.pauseInterval = kDefaultPauseTime;
self.labelSpacing = kDefaultLabelBufferSpace;
self.textAlignment = NSTextAlignmentLeft;
self.animationOptions = UIViewAnimationOptionCurveLinear;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.scrollEnabled = NO;
self.userInteractionEnabled = NO;
self.backgroundColor = [UIColor clearColor];
self.clipsToBounds = YES;
self.fadeLength = kDefaultFadeLength;
}
- (void)dealloc {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)setFrame:(CGRect)frame {
[super setFrame:frame];
[self didChangeFrame];
}
// For autolayout
- (void)setBounds:(CGRect)bounds {
[super setBounds:bounds];
[self didChangeFrame];
}
- (void)didMoveToWindow {
[super didMoveToWindow];
if (self.window) {
[self scrollLabelIfNeeded];
}
}
#pragma mark - Properties
- (UIScrollView *)scrollView {
if (_scrollView == nil) {
_scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
_scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
_scrollView.backgroundColor = [UIColor clearColor];
[self addSubview:_scrollView];
}
return _scrollView;
}
- (void)setFadeLength:(CGFloat)fadeLength {
if (_fadeLength != fadeLength) {
_fadeLength = fadeLength;
[self refreshLabels];
[self applyGradientMaskForFadeLength:fadeLength enableFade:NO];
}
}
- (UILabel *)mainLabel {
return self.labels[0];
}
- (void)setText:(NSString *)theText {
[self setText:theText refreshLabels:YES];
}
- (void)setText:(NSString *)theText refreshLabels:(BOOL)refresh {
// ignore identical text changes
if ([theText isEqualToString:self.text])
return;
EACH_LABEL(text, theText)
if (refresh)
[self refreshLabels];
}
- (NSString *)text {
return self.mainLabel.text;
}
- (void)setAttributedText:(NSAttributedString *)theText {
[self setAttributedText:theText refreshLabels:YES];
}
- (void)setAttributedText:(NSAttributedString *)theText refreshLabels:(BOOL)refresh {
// ignore identical text changes
if ([theText.string isEqualToString:self.attributedText.string])
return;
EACH_LABEL(attributedText, theText)
if (refresh)
[self refreshLabels];
}
- (NSAttributedString *)attributedText {
return self.mainLabel.attributedText;
}
- (void)setTextColor:(UIColor *)color {
EACH_LABEL(textColor, color)
}
- (UIColor *)textColor {
return self.mainLabel.textColor;
}
- (void)setFont:(UIFont *)font {
if (self.mainLabel.font == font)
return;
EACH_LABEL(font, font)
[self refreshLabels];
[self invalidateIntrinsicContentSize];
}
- (UIFont *)font {
return self.mainLabel.font;
}
- (void)setScrollSpeed:(float)speed {
_scrollSpeed = speed;
[self scrollLabelIfNeeded];
}
- (void)setScrollDirection:(QZAutoScrollDirection)direction {
_scrollDirection = direction;
[self scrollLabelIfNeeded];
}
- (void)setShadowColor:(UIColor *)color {
EACH_LABEL(shadowColor, color)
}
- (UIColor *)shadowColor {
return self.mainLabel.shadowColor;
}
- (void)setShadowOffset:(CGSize)offset {
EACH_LABEL(shadowOffset, offset)
}
- (CGSize)shadowOffset {
return self.mainLabel.shadowOffset;
}
#pragma mark - Autolayout
- (CGSize)intrinsicContentSize {
return CGSizeMake(0, [self.mainLabel intrinsicContentSize].height);
}
#pragma mark - Misc
- (void)observeApplicationNotifications {
[[NSNotificationCenter defaultCenter] removeObserver:self];
// restart scrolling when the app has been activated
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(scrollLabelIfNeeded)
name:UIApplicationWillEnterForegroundNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(scrollLabelIfNeeded)
name:UIApplicationDidBecomeActiveNotification
object:nil];
#ifndef TARGET_OS_TV
// refresh labels when interface orientation is changed
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onUIApplicationDidChangeStatusBarOrientationNotification:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
#endif
}
- (void)enableShadow {
_scrolling = YES;
[self applyGradientMaskForFadeLength:self.fadeLength enableFade:YES];
}
- (void)scrollLabelIfNeeded {
if (!self.text.length)
return;
CGFloat labelWidth = CGRectGetWidth(self.mainLabel.bounds);
if (labelWidth <= CGRectGetWidth(self.bounds))
return;
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(scrollLabelIfNeeded) object:nil];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(enableShadow) object:nil];
[self.scrollView.layer removeAllAnimations];
BOOL doScrollLeft = (self.scrollDirection == QZAutoScrollDirectionLeft);
self.scrollView.contentOffset = (doScrollLeft ? CGPointZero : CGPointMake(labelWidth + self.labelSpacing, 0));
// Add the left shadow after delay
[self performSelector:@selector(enableShadow) withObject:nil afterDelay:self.pauseInterval];
// animate the scrolling
NSTimeInterval duration = labelWidth / self.scrollSpeed;
[UIView animateWithDuration:duration delay:self.pauseInterval options:self.animationOptions | UIViewAnimationOptionAllowUserInteraction animations:^{
// adjust offset
self.scrollView.contentOffset = (doScrollLeft ? CGPointMake(labelWidth + self.labelSpacing, 0) : CGPointZero);
} completion:^(BOOL finished) {
_scrolling = NO;
// remove the left shadow
[self applyGradientMaskForFadeLength:self.fadeLength enableFade:NO];
// setup pause delay/loop
if (finished) {
[self performSelector:@selector(scrollLabelIfNeeded) withObject:nil];
}
}];
}
- (void)refreshLabels {
__block float offset = 0;
each_object(self.labels, ^(UILabel *label) {
[label sizeToFit];
CGRect frame = label.frame;
frame.origin = CGPointMake(offset, 0);
frame.size.height = CGRectGetHeight(self.bounds);
label.frame = frame;
// Recenter label vertically within the scroll view
label.center = CGPointMake(label.center.x, roundf(self.center.y - CGRectGetMinY(self.frame)));
offset += CGRectGetWidth(label.bounds) + self.labelSpacing;
});
self.scrollView.contentOffset = CGPointZero;
[self.scrollView.layer removeAllAnimations];
// if the label is bigger than the space allocated, then it should scroll
if (CGRectGetWidth(self.mainLabel.bounds) > CGRectGetWidth(self.bounds)) {
CGSize size;
size.width = CGRectGetWidth(self.mainLabel.bounds) + CGRectGetWidth(self.bounds) + self.labelSpacing;
size.height = CGRectGetHeight(self.bounds);
self.scrollView.contentSize = size;
EACH_LABEL(hidden, NO)
[self applyGradientMaskForFadeLength:self.fadeLength enableFade:self.scrolling];
[self scrollLabelIfNeeded];
} else {
// Hide the other labels
EACH_LABEL(hidden, (self.mainLabel != label))
// adjust the scroll view and main label
self.scrollView.contentSize = self.bounds.size;
self.mainLabel.frame = self.bounds;
self.mainLabel.hidden = NO;
self.mainLabel.textAlignment = self.textAlignment;
// cleanup animation
[self.scrollView.layer removeAllAnimations];
[self applyGradientMaskForFadeLength:0 enableFade:NO];
}
}
// bounds or frame has been changed
- (void)didChangeFrame {
[self refreshLabels];
[self applyGradientMaskForFadeLength:self.fadeLength enableFade:self.scrolling];
}
#pragma mark - Gradient
// ref: https://github.com/cbpowell/MarqueeLabel
- (void)applyGradientMaskForFadeLength:(CGFloat)fadeLength enableFade:(BOOL)fade {
CGFloat labelWidth = CGRectGetWidth(self.mainLabel.bounds);
if (labelWidth <= CGRectGetWidth(self.bounds))
fadeLength = 0;
if (fadeLength) {
// Recreate gradient mask with new fade length
CAGradientLayer *gradientMask = [CAGradientLayer layer];
gradientMask.bounds = self.layer.bounds;
gradientMask.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
gradientMask.shouldRasterize = YES;
gradientMask.rasterizationScale = [UIScreen mainScreen].scale;
gradientMask.startPoint = CGPointMake(0, CGRectGetMidY(self.frame));
gradientMask.endPoint = CGPointMake(1, CGRectGetMidY(self.frame));
// setup fade mask colors and location
id transparent = (id)[UIColor clearColor].CGColor;
id opaque = (id)[UIColor blackColor].CGColor;
gradientMask.colors = @[transparent, opaque, opaque, transparent];
// calcluate fade
CGFloat fadePoint = fadeLength / CGRectGetWidth(self.bounds);
NSNumber *leftFadePoint = @(fadePoint);
NSNumber *rightFadePoint = @(1 - fadePoint);
if (!fade) switch (self.scrollDirection) {
case QZAutoScrollDirectionLeft:
leftFadePoint = @0;
break;
case QZAutoScrollDirectionRight:
leftFadePoint = @0;
rightFadePoint = @1;
break;
}
// apply calculations to mask
gradientMask.locations = @[@0, leftFadePoint, rightFadePoint, @1];
// don't animate the mask change
[CATransaction begin];
[CATransaction setDisableActions:YES];
self.layer.mask = gradientMask;
[CATransaction commit];
} else {
// Remove gradient mask for 0.0f length fade length
self.layer.mask = nil;
}
}
#pragma mark - Notifications
- (void)onUIApplicationDidChangeStatusBarOrientationNotification:(NSNotification *)notification {
// delay to have it re-calculate on next runloop
[self performSelector:@selector(refreshLabels) withObject:nil afterDelay:.1f];
[self performSelector:@selector(scrollLabelIfNeeded) withObject:nil afterDelay:.1f];
}
@end