From 32f7aff775c7dae8a3ee224e9ee99e273e9f65ba Mon Sep 17 00:00:00 2001 From: "C. Bess" Date: Wed, 14 Aug 2013 10:50:44 -0500 Subject: [PATCH 1/3] - allow observation of respective notifications to keep the label in a more predictable state. Compliments info from #23. --- CBAutoScrollLabel/CBAutoScrollLabel.h | 8 ++++++++ CBAutoScrollLabel/CBAutoScrollLabel.m | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/CBAutoScrollLabel/CBAutoScrollLabel.h b/CBAutoScrollLabel/CBAutoScrollLabel.h index 6da4b80..2b6bfb0 100644 --- a/CBAutoScrollLabel/CBAutoScrollLabel.h +++ b/CBAutoScrollLabel/CBAutoScrollLabel.h @@ -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 diff --git a/CBAutoScrollLabel/CBAutoScrollLabel.m b/CBAutoScrollLabel/CBAutoScrollLabel.m index 72ceac6..d55c003 100644 --- a/CBAutoScrollLabel/CBAutoScrollLabel.m +++ b/CBAutoScrollLabel/CBAutoScrollLabel.m @@ -116,6 +116,8 @@ - (void)dealloc { self.labels = nil; [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(enableShadow) object:nil]; + [[NSNotificationCenter defaultCenter] removeObserver:self]; + #if ! __has_feature(objc_arc) [super dealloc]; #endif @@ -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(refreshLabels) + name:UIApplicationDidChangeStatusBarFrameNotification + object:nil]; +} + - (void)enableShadow { _scrolling = YES; From aafe6c96cd91a6a84be2857405d9721079ec1ce7 Mon Sep 17 00:00:00 2001 From: "C. Bess" Date: Wed, 14 Aug 2013 10:55:50 -0500 Subject: [PATCH 2/3] - disable implicit animation when update gradient mask --- CBAutoScrollLabel/CBAutoScrollLabel.m | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/CBAutoScrollLabel/CBAutoScrollLabel.m b/CBAutoScrollLabel/CBAutoScrollLabel.m index d55c003..7506eef 100644 --- a/CBAutoScrollLabel/CBAutoScrollLabel.m +++ b/CBAutoScrollLabel/CBAutoScrollLabel.m @@ -336,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 @@ -346,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 @@ -393,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) { @@ -406,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; @@ -433,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 { From 004b1737bfebb39c4c1560d79c2985c571552948 Mon Sep 17 00:00:00 2001 From: "C. Bess" Date: Wed, 14 Aug 2013 12:14:54 -0500 Subject: [PATCH 3/3] - update demo app - update app auto-resizing masks - fix rotation notification actions, add calculation delay --- .../AutoScrollLabelDemo/ASLViewController.m | 6 ++++-- .../en.lproj/ASLViewController.xib | 6 +++--- CBAutoScrollLabel/CBAutoScrollLabel.m | 15 ++++++++++++--- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/AutoScrollLabelDemo/AutoScrollLabelDemo/ASLViewController.m b/AutoScrollLabelDemo/AutoScrollLabelDemo/ASLViewController.m index 7e75059..9fdbc09 100644 --- a/AutoScrollLabelDemo/AutoScrollLabelDemo/ASLViewController.m +++ b/AutoScrollLabelDemo/AutoScrollLabelDemo/ASLViewController.m @@ -24,7 +24,7 @@ - (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 @@ -32,14 +32,16 @@ - (void)viewDidLoad 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 diff --git a/AutoScrollLabelDemo/AutoScrollLabelDemo/en.lproj/ASLViewController.xib b/AutoScrollLabelDemo/AutoScrollLabelDemo/en.lproj/ASLViewController.xib index 5f0fc25..6b282d5 100644 --- a/AutoScrollLabelDemo/AutoScrollLabelDemo/en.lproj/ASLViewController.xib +++ b/AutoScrollLabelDemo/AutoScrollLabelDemo/en.lproj/ASLViewController.xib @@ -40,7 +40,7 @@ - 292 + 293 {{20, 196}, {280, 21}} @@ -73,7 +73,7 @@ - 292 + 290 {{20, 105}, {280, 42}} @@ -95,7 +95,7 @@ - 292 + 290 {{75, 6}, {170, 33}} diff --git a/CBAutoScrollLabel/CBAutoScrollLabel.m b/CBAutoScrollLabel/CBAutoScrollLabel.m index 7506eef..8c0975c 100644 --- a/CBAutoScrollLabel/CBAutoScrollLabel.m +++ b/CBAutoScrollLabel/CBAutoScrollLabel.m @@ -115,7 +115,7 @@ - (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) @@ -286,8 +286,8 @@ - (void)observeApplicationNotifications // refresh labels when interface orientation is changed [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(refreshLabels) - name:UIApplicationDidChangeStatusBarFrameNotification + selector:@selector(onUIApplicationDidChangeStatusBarOrientationNotification:) + name:UIApplicationDidChangeStatusBarOrientationNotification object:nil]; } @@ -445,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