Skip to content

Commit a2ec26b

Browse files
committed
Handle case where a subclass/something shuts the label down between animations [Common]
1 parent 62a6ddf commit a2ec26b

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

Sources/ObjC/MarqueeLabel.m

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -576,22 +576,38 @@ - (void)scrollAwayWithInterval:(NSTimeInterval)interval delayAmount:(NSTimeInter
576576

577577
__weak __typeof__(self) weakSelf = self;
578578
self.scrollCompletionBlock = ^(BOOL finished) {
579-
if (!finished || !weakSelf) {
580-
// Do not continue into the next loop
579+
if (!weakSelf) {
581580
return;
582581
}
582+
583583
// Call returned home method
584584
[weakSelf labelReturnedToHome:YES];
585+
585586
// Check to ensure that:
586-
// 1) We don't double fire if an animation already exists
587-
// 2) The instance is still attached to a window - this completion block is called for
587+
// 1) The instance is still attached to a window - this completion block is called for
588588
// many reasons, including if the animation is removed due to the view being removed
589589
// from the UIWindow (typically when the view controller is no longer the "top" view)
590-
if (self.window && ![weakSelf.subLabel.layer animationForKey:@"position"]) {
591-
// Begin again, if conditions met
592-
if (weakSelf.labelShouldScroll && !weakSelf.tapToScroll && !weakSelf.holdScrolling) {
593-
[weakSelf scrollAwayWithInterval:interval delayAmount:delayAmount shouldReturn:shouldReturn];
594-
}
590+
if (!weakSelf.window) {
591+
return;
592+
}
593+
// 2) We don't double fire if an animation already exists
594+
if ([weakSelf.subLabel.layer animationForKey:@"position"]) {
595+
return;
596+
}
597+
// 3) We don't not start automatically if the animation was unexpectedly interrupted
598+
if (!finished) {
599+
// Do not continue into the next loop
600+
return;
601+
}
602+
// 4) A completion block still exists for the NEXT loop. A notable case here is if
603+
// returnLabelToHome was called during a subclass's labelReturnToHome function
604+
if (!weakSelf.scrollCompletionBlock) {
605+
return;
606+
}
607+
608+
// Begin again, if conditions met
609+
if (weakSelf.labelShouldScroll && !weakSelf.tapToScroll && !weakSelf.holdScrolling) {
610+
[weakSelf scrollAwayWithInterval:interval delayAmount:delayAmount shouldReturn:shouldReturn];
595611
}
596612
};
597613

Sources/Swift/MarqueeLabel.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -840,11 +840,6 @@ open class MarqueeLabel: UILabel, CAAnimationDelegate {
840840
#endif
841841

842842
scrollCompletionBlock = { [weak self] (finished: Bool) -> () in
843-
guard finished else {
844-
// Do not continue into the next loop
845-
return
846-
}
847-
848843
guard (self != nil) else {
849844
return
850845
}
@@ -853,17 +848,27 @@ open class MarqueeLabel: UILabel, CAAnimationDelegate {
853848
self!.labelReturnedToHome(true)
854849

855850
// Check to ensure that:
856-
// 1) We don't double fire if an animation already exists
857-
// 2) The instance is still attached to a window - this completion block is called for
851+
852+
// 1) The instance is still attached to a window - this completion block is called for
858853
// many reasons, including if the animation is removed due to the view being removed
859854
// from the UIWindow (typically when the view controller is no longer the "top" view)
860855
guard self!.window != nil else {
861856
return
862857
}
863-
858+
// 2) We don't double fire if an animation already exists
864859
guard self!.sublabel.layer.animation(forKey: "position") == nil else {
865860
return
866861
}
862+
// 3) We don't not start automatically if the animation was unexpectedly interrupted
863+
guard finished else {
864+
// Do not continue into the next loop
865+
return
866+
}
867+
// 4) A completion block still exists for the NEXT loop. A notable case here is if
868+
// returnLabelToHome() was called during a subclass's labelReturnToHome() function
869+
guard (self!.scrollCompletionBlock != nil) else {
870+
return
871+
}
867872

868873
// Begin again, if conditions met
869874
if (self!.labelShouldScroll() && !self!.tapToScroll && !self!.holdScrolling) {

0 commit comments

Comments
 (0)