diff --git a/AutoScrollLabelDemo/AutoScrollLabelDemo/ASLViewController.m b/AutoScrollLabelDemo/AutoScrollLabelDemo/ASLViewController.m index fef8fcd..6d82baf 100644 --- a/AutoScrollLabelDemo/AutoScrollLabelDemo/ASLViewController.m +++ b/AutoScrollLabelDemo/AutoScrollLabelDemo/ASLViewController.m @@ -40,4 +40,20 @@ - (void)viewDidLoad { [self.navigationBarScrollLabel observeApplicationNotifications]; } +- (IBAction)stopScrollingAfterCyle:(id)sender { + [self.autoScrollLabel stopScrollingAfterCurrentCycle]; +} + +- (IBAction)stopScrollingImmediately:(id)sender { + [self.autoScrollLabel stopScrollingImmediatelyAnimated:NO]; +} + +- (IBAction)stopScrollingImmediatelyAnimated:(id)sender { + [self.autoScrollLabel stopScrollingImmediatelyAnimated:YES]; +} + +- (IBAction)restartScrolling:(id)sender { + [self.autoScrollLabel reenableAutoScroll]; +} + @end diff --git a/AutoScrollLabelDemo/AutoScrollLabelDemo/en.lproj/Main.storyboard b/AutoScrollLabelDemo/AutoScrollLabelDemo/en.lproj/Main.storyboard index cbac453..838bece 100644 --- a/AutoScrollLabelDemo/AutoScrollLabelDemo/en.lproj/Main.storyboard +++ b/AutoScrollLabelDemo/AutoScrollLabelDemo/en.lproj/Main.storyboard @@ -1,8 +1,9 @@ - + - + + @@ -17,24 +18,81 @@ - + + + + + + + + + + + + + + + + + + - + diff --git a/AutoScrollLabelDemo/screenshot.png b/AutoScrollLabelDemo/screenshot.png index 5730fbd..73667fc 100644 Binary files a/AutoScrollLabelDemo/screenshot.png and b/AutoScrollLabelDemo/screenshot.png differ diff --git a/CBAutoScrollLabel/CBAutoScrollLabel.h b/CBAutoScrollLabel/CBAutoScrollLabel.h index 9a20b4c..17eec44 100644 --- a/CBAutoScrollLabel/CBAutoScrollLabel.h +++ b/CBAutoScrollLabel/CBAutoScrollLabel.h @@ -36,6 +36,7 @@ typedef NS_ENUM(NSInteger, CBAutoScrollDirection) { * Returns YES, if it is actively scrolling, NO if it has paused or if text is within bounds (disables scrolling). */ @property (nonatomic, readonly) BOOL scrolling; +@property (nonatomic, assign, readonly) BOOL shouldScroll; @property (nonatomic) CGFloat fadeLength; // UILabel properties @@ -65,6 +66,22 @@ typedef NS_ENUM(NSInteger, CBAutoScrollDirection) { */ - (void)scrollLabelIfNeeded; +/** + * Stops auto-scroll after the current cycle has been finished. + */ +- (void)stopScrollingAfterCurrentCycle; + +/** + * Stops auto-scroll immediately and resets the scroll position to the start. + * @param animated Defines whether the scrolling to the beginning should be animated or not. + */ +- (void)stopScrollingImmediatelyAnimated:(BOOL)animated; + +/** + * (Re)enables auto-scroll. If auto-scroll is already enabled, calling this method has no effect. + */ +- (void)reenableAutoScroll; + /** * Observes UIApplication state notifications to auto-restart scrolling and watch for * orientation changes to refresh the labels. diff --git a/CBAutoScrollLabel/CBAutoScrollLabel.m b/CBAutoScrollLabel/CBAutoScrollLabel.m index 5663f26..d0799d3 100644 --- a/CBAutoScrollLabel/CBAutoScrollLabel.m +++ b/CBAutoScrollLabel/CBAutoScrollLabel.m @@ -35,6 +35,7 @@ @interface CBAutoScrollLabel () @property (nonatomic, strong) NSArray *labels; @property (nonatomic, strong, readonly) UILabel *mainLabel; @property (nonatomic, strong) UIScrollView *scrollView; +@property (nonatomic, assign, readwrite) BOOL shouldScroll; @end @@ -84,6 +85,7 @@ - (void)commonInit { self.backgroundColor = [UIColor clearColor]; self.clipsToBounds = YES; self.fadeLength = kDefaultFadeLength; + self.shouldScroll = YES; } - (void)dealloc { @@ -253,7 +255,7 @@ - (void)enableShadow { } - (void)scrollLabelIfNeeded { - if (!self.text.length) + if (!self.text.length || !self.shouldScroll) return; CGFloat labelWidth = CGRectGetWidth(self.mainLabel.bounds); @@ -289,6 +291,23 @@ - (void)scrollLabelIfNeeded { }]; } +- (void)stopScrollingAfterCurrentCycle { + self.shouldScroll = NO; +} + +- (void)stopScrollingImmediatelyAnimated:(BOOL)animated { + self.shouldScroll = NO; + [self.scrollView.layer removeAllAnimations]; + [self.scrollView setContentOffset:CGPointZero animated:animated]; +} + +- (void)reenableAutoScroll { + self.shouldScroll = YES; + if (!self.scrolling) { + [self scrollLabelIfNeeded]; + } +} + - (void)refreshLabels { __block float offset = 0;