1

I am writing an app using a cocoa control that animates text,

https://www.cocoacontrols.com/controls/marqueelabel

It uses CoreText and QuartzCore.

It's a pretty great control, but i'm having trouble. When instantiated the animated labels that I create using the control start to scroll immediately, however I'm finding that when I create an animated label using NSTimer it is not animating. I am using

- (void)viewDidLoad
{

    [super viewDidLoad];

    ....
    [self.view addSubview:continuousLabel1];
    [NSTimer scheduledTimerWithTimeInterval:5
                                                target:self
                                              selector:@selector(updatePrice)
                                              userInfo:nil repeats:YES];


}

to call a method that creates the animated label, when I call the method directly the labels create and animate, however when called using the above scheduled timer, new labels created only animate on the first call, not on the timers repeat calls to the method. I have checked that my method is being call on the main thread/ main runloop, any ideas?

For clarity the work flow is:

Timer calls method --> Label 1 is created and starts to Animate.

5 Seconds Late...

Timer calls method again --> Label 2 is created However is does not begin to animate as expected.

EDIT:

- (void)updatePrice
{


        MarqueeLabel *continuousLabel2 = [[MarqueeLabel alloc] initWithFrame:CGRectMake(10, 230, self.view.frame.size.width-20, 20) rate:100.0f andFadeLength:10.0f];
        continuousLabel2.tag = 101;
        continuousLabel2.marqueeType = MLContinuous;
        continuousLabel2.animationCurve = UIViewAnimationOptionCurveLinear;
        continuousLabel2.continuousMarqueeExtraBuffer = 50.0f;
        continuousLabel2.numberOfLines = 1;
        continuousLabel2.opaque = NO;
        continuousLabel2.enabled = YES;
        continuousLabel2.shadowOffset = CGSizeMake(0.0, -1.0);
        continuousLabel2.textAlignment = NSTextAlignmentLeft;
        continuousLabel2.textColor = [UIColor colorWithRed:0.234 green:0.234 blue:0.234 alpha:1.000];
        continuousLabel2.backgroundColor = [UIColor clearColor];
        continuousLabel2.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.000];
        continuousLabel2.text = @"This is another long label that doesn't scroll continuously with a custom space between labels! You can also tap it to pause and unpause it!";

        [self.view addSubview:continuousLabel2];

    if( [NSThread isMainThread])
    {
        NSLog(@"In Main Thread");
    }
    if ([NSRunLoop currentRunLoop] == [NSRunLoop mainRunLoop]) {
        NSLog(@"In Main Loop");
    }


}
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
Woodstock
  • 22,184
  • 15
  • 80
  • 118
  • 1
    yesterday only I gave an answer on [marquee](http://stackoverflow.com/questions/16860254/how-to-make-marquee-uilabel-uitextfield-nstextfield/16860529#16860529) – Anoop Vaidya Jun 01 '13 at 09:00
  • 1
    Did you even read the question? I know exactly how to create a Marquee, my question is not a duplicate or even remotely similar to the one you suggested. The animations on the label fail to occur when the labels are instantiated by the NSTimers repeats calls. – Woodstock Jun 01 '13 at 09:14
  • as your problem is not shown in the code, I assumed that answer will help you. One difference I see in I had a property that started and invalidated the timer. in your case i am not sure from where and how many times you are calling `myMethod`.... And linking and dupes can be 80-100% simlar to what you want. – Anoop Vaidya Jun 01 '13 at 09:45
  • Anoop, it's like we're talking about totally different questions.[NSTimer scheduledTimerWithTimeInterval, calls myMethod every 5 seconds, also you don't have to invalidate it as it's a class method.] – Woodstock Jun 01 '13 at 10:07
  • Anyone have any ideas why when the timer repeat fires, coreanimations fail in the selector method? – Woodstock Jun 01 '13 at 10:07
  • Hi Anoop, here is a project that shows exactly what I'm asking! http://ge.tt/1gkqzFi/v/0?c Any idea why the second label doesn't animate? – Woodstock Jun 02 '13 at 06:59
  • I checked and feeling sorry for closing your question as dupe. It happened only due to incomplete question. You question should be like this. – Anoop Vaidya Jun 02 '13 at 07:44

2 Answers2

2

The problem is that you are calling updatePrice every 5 seconds and in this method you are creating a new label, that that is stacking up, and notice that it is going darker and darker every 5 seconds.

And if you check the label is scrolling.

Solution:

If you want multiple instances of the label change its coordinates.

If you want single instance then compare check and create it just once.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • Hi Anoop! Thanks for your help, now that you have the project you can see my problem, I understand the label is stacking if you turn off repeat on the timer it will not stack, that is ok as this is just an example project, my problem is the top label is scrolling but the bottom label does not scroll for me! Look at this screenshot to see what I mean, and thank you again for your time... Thank you Sir, http://i.imgur.com/J36jDb5.png – Woodstock Jun 02 '13 at 08:33
  • Here I have turned the repeat off on the timer, and still the second label created does not animate! – Woodstock Jun 02 '13 at 08:38
  • click on the button of ios similator, then clik on the app, it will start animating. – Anoop Vaidya Jun 02 '13 at 09:45
  • i am not into ios. but i guess due to `viewDidLoad` method, try pulling it out to some other method. I guess the timer fires once. – Anoop Vaidya Jun 02 '13 at 10:11
1

You should read the section Important Animation Note from the README file.

My guess is that you will need to call controllerLabelsShouldAnimate: passing your current view controller to animate those labels added after the view controller has already been shown.

yonosoytu
  • 3,319
  • 1
  • 17
  • 23
  • Thank you! You have fixed my problem! The problem was MarqueeLabel is based on UIView animations, which causes some problems when views appear and disappear and the repeating animation is stopped by iOS and does not automatically restart. In this case I had not changed view but somehow adding the label by timer seems to cause the problem with animation. This code 'MarqueeLabel controllerViewAppearing:self];' fixed my issue! Thanks! ps. rptwsthi, soon, Roman C, Graviton THIS QUESTION IS NOT A DUPLICATION. If you spent more time helping and less time looking for points you might have noticed – Woodstock Jun 03 '13 at 16:05
  • yonosoytu - I would upvote your answer but due to an arbitrary restriction I cannot. Maybe one of the helpful chaps that marked it INCORRECTLY as a dupe of a totally unrelated question might be able to upvote for me. – Woodstock Jun 03 '13 at 16:07