Autoscroll marquee label in iphone

13,338

Solution 1

Check out this open source component, actually called MarqueeLabel.

It makes this really easy.

Solution 2

Inside the Interface Builder (Main.storyboard file) I added a UIView to give him a place,

and I hooked the object in the ViewController.h

IBOutlet UIView *marqueeView;

After, in the ViewController.m, into the viewDidLoad method I have written:

MarqueeLabel *marqueeLabel = [[MarqueeLabel alloc] initWithFrame:CGRectMake(0, 0, 315, 20) duration:8.0 andFadeLength:10.0f];
marqueeLabel.text = @"marquee text.. this text will scroll";
[marqueeView addSubview:marqueeLabel];

I hope you can be useful.

Solution 3

UILabel is a UIView. There are a number of ways to animate a UIView but using a block is probably your best choice

MarqueeLabel *marquee == ...
[UIView animateWithDuration:5.0 animations:^{ 
    CGRect frame = marquee.frame;
    frame.origin.x = -50.0f
    marquee.frame = frame;
}]:

This will have the effect of "scrolling" marquee to the left 50 pixels over 5 seconds. You'll need to make adjustments for the size of the containing view as well as the width of marquee.

If you want the scroll off on the left to immediately appear on the right, you could achieve this with a second marquee that starts its x origin at 320 as the first marquees x origin is a 0.

Solution 4

Its pretty simple. Try the below code,

    [UIView animateKeyframesWithDuration:5 delay:0 options:UIViewKeyframeAnimationOptionRepeat|UIViewAnimationOptionCurveLinear animations:^{
//_mlabel is my created sample label
        _mlabel.frame=CGRectMake(0-(self.view.frame.size.width), _mlabel.frame.origin.y, _mlabel.frame.size.width, _mlabel.frame.size.height);

    } completion:nil];

Hope it helps :)

Share:
13,338
Yogesh Maheshwari
Author by

Yogesh Maheshwari

Updated on June 04, 2022

Comments

  • Yogesh Maheshwari
    Yogesh Maheshwari almost 2 years

    I have an UILabel that I want to scroll like marquee tag in HTML, how am I supposed to do that?

    #define LEFTSCROLLDIRECTION 0
    #define RIGHTSCROLLDIRECTION 1
    
    MarqueeLabel *marquee = [[MarqueeLabel alloc] initWithFrame:CGRectMake(10, 10, 300, 30)];
    label.scrollDirection = LEFTSCROLLDIRECTION;
    [self.view addSubview:marquee];
    

    Edit

    I have found this class for this that seems to work at first..

    But there is a fault in this.. if I navigate forward in UINavigationController and pop that pushedViewController after a time, the autoScrollLabel stops animating..

    AutoScrollLabel *autoScrollLabel = [[AutoScrollLabel alloc] init];
    autoScrollLabel.text = @"Hi Mom!  How are you?  I really ought to write more often.";
    autoScrollLabel.textColor = [UIColor yellowColor];
    
  • bbarnhart
    bbarnhart almost 12 years
    You'll need to do some additional work. Shouldn't be too difficult.
  • bbarnhart
    bbarnhart almost 12 years
    Hint: Look at UIView animateWithDuration:animations:completion:
  • Yogesh Maheshwari
    Yogesh Maheshwari almost 12 years
    What notification do I get when a view appears on screen.. (In my UIView subclass)
  • bbarnhart
    bbarnhart almost 12 years
    Not sure you get a notification when a UIView appears on the screen.
  • Yogesh Maheshwari
    Yogesh Maheshwari almost 12 years
    It is complicated, if the view goes out of the screen, then in completion block I get ('BOOL finished' equal to NO).. meaning animation didn't occur and if I still loop through animation that becomes very heavy process and canceling animation at that time would mean I would have to restart the animation again when that view will again appear on the screen