Skip to content

Commit

Permalink
Merge pull request #24 from cbess/observer-notifications
Browse files Browse the repository at this point in the history
Add observer notifications and update demo app.
  • Loading branch information
cbess committed Aug 16, 2013
2 parents ebec5cb + 004b173 commit 2969c00
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
6 changes: 4 additions & 2 deletions AutoScrollLabelDemo/AutoScrollLabelDemo/ASLViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,24 @@ - (void)viewDidLoad
[super viewDidLoad];

// setup the auto scroll label
self.autoScrollLabel.text = @"This text may be clipped, but now it will be scrolled.";
self.autoScrollLabel.text = @"This text may be clipped, but now it will be scrolled. This text will be scrolled even after device rotation.";
self.autoScrollLabel.textColor = [UIColor blueColor];
self.autoScrollLabel.labelSpacing = 35; // distance between start and end labels
self.autoScrollLabel.pauseInterval = 1.7; // seconds of pause before scrolling starts again
self.autoScrollLabel.scrollSpeed = 30; // pixels per second
self.autoScrollLabel.textAlignment = NSTextAlignmentCenter; // centers text when no auto-scrolling is applied
self.autoScrollLabel.fadeLength = 12.f;
self.autoScrollLabel.scrollDirection = CBAutoScrollDirectionLeft;
[self.autoScrollLabel observeApplicationNotifications];

// navigation bar auto scroll label
self.navigationBarScrollLabel.text = @"Navigation Bar Title... Scrolling.";
self.navigationBarScrollLabel.text = @"Navigation Bar Title... Scrolling... And scrolling.";
self.navigationBarScrollLabel.pauseInterval = 3.f;
self.navigationBarScrollLabel.font = [UIFont boldSystemFontOfSize:20];
self.navigationBarScrollLabel.textColor = [UIColor whiteColor];
self.navigationBarScrollLabel.shadowOffset = CGSizeMake(-1, -1);
self.navigationBarScrollLabel.shadowColor = [UIColor blackColor];
[self.navigationBarScrollLabel observeApplicationNotifications];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<array class="NSMutableArray" key="NSSubviews">
<object class="IBUILabel" id="84194377">
<reference key="NSNextResponder" ref="774585933"/>
<int key="NSvFlags">292</int>
<int key="NSvFlags">293</int>
<string key="NSFrame">{{20, 196}, {280, 21}}</string>
<reference key="NSSuperview" ref="774585933"/>
<reference key="NSWindow"/>
Expand Down Expand Up @@ -73,7 +73,7 @@
</object>
<object class="IBUIView" id="375038343">
<reference key="NSNextResponder" ref="774585933"/>
<int key="NSvFlags">292</int>
<int key="NSvFlags">290</int>
<array class="NSMutableArray" key="NSSubviews"/>
<string key="NSFrame">{{20, 105}, {280, 42}}</string>
<reference key="NSSuperview" ref="774585933"/>
Expand All @@ -95,7 +95,7 @@
<array class="NSMutableArray" key="NSSubviews">
<object class="IBUIView" id="295045083">
<reference key="NSNextResponder" ref="76686105"/>
<int key="NSvFlags">292</int>
<int key="NSvFlags">290</int>
<array class="NSMutableArray" key="NSSubviews"/>
<string key="NSFrame">{{75, 6}, {170, 33}}</string>
<reference key="NSSuperview" ref="76686105"/>
Expand Down
8 changes: 8 additions & 0 deletions CBAutoScrollLabel/CBAutoScrollLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,12 @@ typedef enum {
* Initiates auto-scroll if the label width exceeds the bounds of the scrollview.
*/
- (void)scrollLabelIfNeeded;

/**
* Observes UIApplication state notifications to auto-restart scrolling and watch for
* orientation changes to refresh the labels.
* @discussion Must be called to observe the notifications. Calling multiple times will still only
* register the notifications once.
*/
- (void)observeApplicationNotifications;
@end
48 changes: 42 additions & 6 deletions CBAutoScrollLabel/CBAutoScrollLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ - (void)commonInit
- (void)dealloc
{
self.labels = nil;
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(enableShadow) object:nil];
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[[NSNotificationCenter defaultCenter] removeObserver:self];

#if ! __has_feature(objc_arc)
[super dealloc];
#endif
Expand Down Expand Up @@ -267,6 +269,28 @@ - (CGSize)shadowOffset

#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];

// refresh labels when interface orientation is changed
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onUIApplicationDidChangeStatusBarOrientationNotification:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
}

- (void)enableShadow
{
_scrolling = YES;
Expand Down Expand Up @@ -312,7 +336,7 @@ - (void)scrollLabelIfNeeded

- (void)refreshLabels
{
__block float offset = 0.0;
__block float offset = 0;

// calculate the label size
CGSize labelSize = [self.mainLabel.text sizeWithFont:self.mainLabel.font
Expand All @@ -322,7 +346,7 @@ - (void)refreshLabels
CGRect frame = label.frame;
frame.origin.x = offset;
frame.size.height = CGRectGetHeight(self.bounds);
frame.size.width = labelSize.width + 2 /*Magic number*/;
frame.size.width = labelSize.width + 2.f /*Magic number*/;
label.frame = frame;

// Recenter label vertically within the scroll view
Expand Down Expand Up @@ -369,7 +393,7 @@ - (void)applyGradientMaskForFadeLength:(CGFloat)fadeLength enableFade:(BOOL)fade
{
CGFloat labelWidth = CGRectGetWidth(self.mainLabel.bounds);
if (labelWidth <= CGRectGetWidth(self.bounds))
fadeLength = 0.0;
fadeLength = 0;

if (fadeLength)
{
Expand All @@ -382,8 +406,8 @@ - (void)applyGradientMaskForFadeLength:(CGFloat)fadeLength enableFade:(BOOL)fade
gradientMask.shouldRasterize = YES;
gradientMask.rasterizationScale = [UIScreen mainScreen].scale;

gradientMask.startPoint = CGPointMake(0.0, CGRectGetMidY(self.frame));
gradientMask.endPoint = CGPointMake(1.0, CGRectGetMidY(self.frame));
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;
Expand All @@ -409,7 +433,10 @@ - (void)applyGradientMaskForFadeLength:(CGFloat)fadeLength enableFade:(BOOL)fade
// apply calculations to mask
gradientMask.locations = @[@0, leftFadePoint, rightFadePoint, @1];

[CATransaction begin];
[CATransaction setDisableActions:YES];
self.layer.mask = gradientMask;
[CATransaction commit];
}
else
{
Expand All @@ -418,4 +445,13 @@ - (void)applyGradientMaskForFadeLength:(CGFloat)fadeLength enableFade:(BOOL)fade
}
}

#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

0 comments on commit 2969c00

Please sign in to comment.