Hope you can help or explain what's going on here, as I'm not great with UIGraphics
contexts.
I have defined a class that automatically scrolls a label back and forth in it's frame when the label text is to long for the given space on the view controllers primary view.
It consists of a scrollview that fits the allocated space in the parent view, and contains a UILabel
(subclassed) that is sized to fit it's text.
(The UILabel is a subview in the parent scrollview).
The scrollview commits an animation that scrolls the UILabel left -> right, then when the animation is finished, I fire the delegate '
(void)animationDidStop:animationID(NSString*)finished:(NSNumber *)finished context:(void *)context'
which resets some numbers and restarted the animation scrolling it in the reverse direction right -> left
This works really nicely.
However, if i add another instance of this 'scrolling label' somewhere else in the view controllers master view, as one of the animations stop and the AnimationDidStop
delegate gets fired, it relaunches the animations from scratch for the other 'scrolling' label object.
I have tried the following to isolate the firing of the animation with no positive results.
Passing the views context and an identifier to beginAnimations and then trapping them in the (void)animationDidStop
method... Didn't make any difference.
This makes me think the method doesn't seem to take
Below is the code that does the animation, and the code that relaunches it in the other direction.
As I say, it works really well on it's own, but when there is more than 1 instance of this class resident on the screen at the same time, the beginAnimations seems to fire for both instances.
Hope you can explain why. Thanks.
- (void)beginAnimationWithOrgigin:(CGPoint)origin Terminus:(CGPoint)terminus {
NSLog (@"Message %@ in context %@",((UILabel*)_textLabel).text,_ctx);
CGFloat text_width = ((UILabel*)_textLabel).frame.size.width;
CGFloat display_width = self.frame.size.width;
if ( text_width > display_width ) {
float duration = (text_width - display_width)/40;
[self setContentOffset:origin];
[UIView beginAnimations:((UILabel*)_textLabel).text context:_ctx];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelay:1.0];
[UIView setAnimationDuration:duration];
[UIView setAnimationRepeatCount:1];
[self setContentOffset:terminus];
[UIView commitAnimations];
}
}
- (void)animationDidStop:(NSString *)animationID
finished:(NSNumber *)finished
context:(void *)context
{
NSLog(@"Animation Did stop context = %@",context);
if ([animationID isEqualToString:((UILabel*)_textLabel).text]){
static BOOL forward = NO;
if ([finished boolValue]) {
CGPoint origin;
CGPoint terminal_origin;
if (forward){
origin = CGPointMake(0, 0);
terminal_origin = CGPointMake(((UILabel*)_textLabel).frame.size.width - self.frame.size.width, ((UILabel*)_textLabel).frame.origin.y);
}else{
terminal_origin = CGPointMake(0, 0);
origin = CGPointMake(((UILabel*)_textLabel).frame.size.width - self.frame.size.width, ((UILabel*)_textLabel).frame.origin.y);
}
forward = !forward;
[self beginAnimationWithOrgigin:origin Terminus:terminal_origin];
}
}
}
EDIT: Added 28/5/14 @ 13:14 After A-Lives advise regarding the use of statics... have modified the code to use it's label as a flag to prompt the change of direction in scrolling. THIS HAS FIXED THE PROBLEM
- (void)beginAnimationWithOrgigin:(CGPoint)origin Terminus:(CGPoint)terminus Direction:(NSString*)direction{
if (!direction)direction = @"FORWARD";
NSLog (@"Message %@ in context %@",((UILabel*)_textLabel).text,_ctx);
CGFloat text_width = ((UILabel*)_textLabel).frame.size.width;
CGFloat display_width = self.frame.size.width;
if ( text_width > display_width ) {
float duration = (text_width - display_width)/40;
[self setContentOffset:origin];
[UIView beginAnimations:direction context:_ctx];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelay:1.0];
[UIView setAnimationDuration:duration];
[UIView setAnimationRepeatCount:1];
[self setContentOffset:terminus];
[UIView commitAnimations];
}
}
- (void)animationDidStop:(NSString *)animationID
finished:(NSNumber *)finished
context:(void *)context
{
NSLog(@"Animation Did stop context = %@",context);
NSString *direction;
CGPoint origin;
CGPoint terminal_origin;
if (![animationID isEqualToString:@"FORWARD"]){
origin = CGPointMake(0, 0);
terminal_origin = CGPointMake(((UILabel*)_textLabel).frame.size.width - self.frame.size.width, ((UILabel*)_textLabel).frame.origin.y);
direction = @"FORWARD";
}else{
terminal_origin = CGPointMake(0, 0);
origin = CGPointMake(((UILabel*)_textLabel).frame.size.width - self.frame.size.width, ((UILabel*)_textLabel).frame.origin.y);
direction = @"BACKWARD";
}
[self beginAnimationWithOrgigin:origin Terminus:terminal_origin Direction:direction];
}