views:

616

answers:

2

Does anyone know how to create a "text crawl" like that shown at the bottom of the ESPN ScoreCenter app for the iphone?

Basically, there is a small bar of text at the bottom of the screen and the text is animated so that it moves slowly from right to left across the screen.

alt text

Can anyone point me to some source code that does something similar? Is it simply a UIWebView?

I've tried to google this, but to no avail.

Thanks to anyone who can point in the right direction!

+4  A: 

Have you looked at animations in the documentation? This could be solved in a number of ways. If you created this as a UILabel, you could simply move it in an animation (CoreAnimation) using something similar to the following:

_label.frame = ???   // set it to the origin (right edge) 

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:6.0];   // 6 seconds, adjust
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

_label.frame = ???  // set it to the destination (left edge)

[UIView commitAnimations];
marcc
thank you very much!
Additionally if you use an explicit animation (CABasicAnimation or CAKeyframeAnimation) on the UILabel layer, you can set a repeat count that will cause the animation to start over when it completes as may times as you specify.
Matt Long
+3  A: 

I'd recommend creating a UIView subclass. In your init method, set self.clipsToBounds=YES and add two UILabels, both with the same text (whatever you want to scroll). The two are so that you can seamlessly animate them -- put one immediately to the right of the other so that, when they scroll, it looks like the message repeats. As soon as the label scrolls all the way to the left, move it to the right of the other one. This will give you the illusion of seamless scrolling.

You can then use [UIView beginAnimations:] etc to setup the animations, and [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(animationDone)] to receive the messages that the animation ended (so that you can move the view over). Just make sure you create the method referenced.

You could also do this with an NSArray of UILabels to display multiple distinct messages, without adding a whole lot of complexity.

Ian Henry