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];
Check out this open source component, actually called MarqueeLabel.
It makes this really easy.
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.
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.
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 :)