From 4e7d9bbfc4e2a0c2e73364e90718f23636fb1cf2 Mon Sep 17 00:00:00 2001 From: Charles Powell Date: Wed, 14 May 2014 18:22:04 -0700 Subject: [PATCH] Added pre- and post-animation hooks for subclass usage. --- MarqueeLabel.h | 28 ++++++++++++++++++++++++++++ MarqueeLabel.m | 28 +++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/MarqueeLabel.h b/MarqueeLabel.h index f46c8a25..2bc830d4 100755 --- a/MarqueeLabel.h +++ b/MarqueeLabel.h @@ -253,6 +253,7 @@ typedef NS_ENUM(NSUInteger, MarqueeType) { - (void)pauseLabel; + /** Un-pauses a previously paused text scrolling animation @see pauseLabel @@ -261,6 +262,33 @@ typedef NS_ENUM(NSUInteger, MarqueeType) { - (void)unpauseLabel; +/** Called when the label animation has finished, and the label is at the home position. + + The default implementation of this method does nothing. Subclasses may override this method in order to perform any custom actions before + the label animation begins. This is only called in the event that the conditions for scrolling to begin are met. + + @since Available in 1.5.0 and later. + */ + +- (void)labelWillBeginScroll; + + +/** Called when the label animation has finished, and the label is at the home position. + + The default implementation of this method does nothing. Subclasses may override this method in order to perform any custom actions after + the label animation is complete, and before the next animation would begin (assuming the conditions are met). + + @param A Boolean that indicates whether or not the scroll animation actually finished before the completion handler was called. + @since Available in 1.5.0 and later. + + @warning This method will be called, and the `finished` parameter will be `NO`, when any property changes that would cause the label scrolling to be + reset are made. This includes changes to label text and font/font size changes. + */ + +- (void)labelReturnedToHome:(BOOL)finished; + + + //////////////////////////////////////////////////////////////////////////////// /// @name Label States //////////////////////////////////////////////////////////////////////////////// diff --git a/MarqueeLabel.m b/MarqueeLabel.m index 6577fbc0..f1994ea6 100755 --- a/MarqueeLabel.m +++ b/MarqueeLabel.m @@ -566,12 +566,13 @@ - (void)scrollAwayWithInterval:(NSTimeInterval)interval delayAmount:(NSTimeInter return; } - // Perform animation - self.awayFromHome = YES; - [self.subLabel.layer removeAllAnimations]; [self.layer removeAllAnimations]; + // Call pre-animation method + [self labelWillBeginScroll]; + + // Animate [UIView animateWithDuration:interval delay:delayAmount options:self.animationCurve @@ -583,6 +584,9 @@ - (void)scrollAwayWithInterval:(NSTimeInterval)interval delayAmount:(NSTimeInter [self scrollHomeWithInterval:interval delayAmount:delayAmount]; } }]; + + // Move to away state + self.awayFromHome = YES; } - (void)scrollHomeWithInterval:(NSTimeInterval)interval { @@ -605,9 +609,13 @@ - (void)scrollHomeWithInterval:(NSTimeInterval)interval delayAmount:(NSTimeInter self.subLabel.frame = self.homeLabelFrame; } completion:^(BOOL finished){ + // Call completion method + [self labelReturnedToHome:finished]; + if (finished) { // Set awayFromHome self.awayFromHome = NO; + if (!self.tapToScroll && !self.holdScrolling) { [self scrollAwayWithInterval:interval]; } @@ -633,6 +641,9 @@ - (void)scrollContinuousWithInterval:(NSTimeInterval)interval after:(NSTimeInter self.awayFromHome = YES; + // Call pre-animation method + [self labelWillBeginScroll]; + // Animate [UIView animateWithDuration:interval delay:delayAmount @@ -646,6 +657,9 @@ - (void)scrollContinuousWithInterval:(NSTimeInterval)interval after:(NSTimeInter } } completion:^(BOOL finished) { + // Call completion method + [self labelReturnedToHome:finished]; + if (finished && !self.tapToScroll && !self.holdScrolling) { self.awayFromHome = NO; [self scrollContinuousWithInterval:interval after:delayAmount]; @@ -723,6 +737,14 @@ - (void)labelWasTapped:(UITapGestureRecognizer *)recognizer { } } +- (void)labelWillBeginScroll { + return; +} + +- (void)labelReturnedToHome:(BOOL)finished { + return; +} + #pragma mark - Modified UILabel Getters/Setters - (NSString *)text {