4

I want to show some text in my app like moving text (Scrolling with animation from right to left). How to do this programmatically?

I took UIViewcontroller. I am developing AVAudioplayer. so in the top side of UIViewController the text will move from right to left.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3222991
  • 341
  • 3
  • 8
  • 16
  • can you please share the screen shot ? Because it will help to understand clearly. – TamilKing Mar 14 '14 at 05:52
  • 1
    Do you want a sort of marquee? If so take a look at this: http://stackoverflow.com/questions/16860254/how-to-make-marquee-uilabel-uitextfield-nstextfield – carloabelli Mar 14 '14 at 05:56
  • @hey Tamilking thanks for your response.But i have not any screen shot.But you see some websites like news websites the top side the news scrolling from right to left?.That type of functionality i need to implement – user3222991 Mar 14 '14 at 05:56

9 Answers9

14

First of all you take a label in your view and set its frame out of view as following.

 - (void)viewDidLoad
{
    [super viewDidLoad];

    la = [[UILabel alloc]initWithFrame:CGRectMake(320, 100, 200, 60)];

    la.text = @"This is my music line";

    [self.view addSubview:la];

    [NSTimer scheduledTimerWithTimeInterval:2.0
                                     target:self
                                   selector:@selector(LabelAnimation)
                                   userInfo:nil
                                    repeats:YES];

}

Now that label give animation as below method called in ViewDidLoad

-(void)LabelAnimation
{
    [UIView animateWithDuration:3.0f delay:0.0f options:UIViewAnimationOptionTransitionNone animations:^{
        la.frame = CGRectMake(-320, 100, 200, 60);
    } completion:^(BOOL finished)
     {
         la.frame = CGRectMake(320, 100, 200, 60);
     }];

}

output is below.

enter image description here

Kirit Modi
  • 23,155
  • 15
  • 89
  • 112
  • thanks for your response. i will try to implement your code.thank u so much brother – user3222991 Mar 14 '14 at 06:26
  • its working very well.but i have big string like "Sachin tendulkar is world number one bats man".This is my text.if i paste the text in label i got dots.so please help me how to fix the text in single line. – user3222991 Mar 14 '14 at 06:52
  • i already gave 320 But still display dots.so how can solve the problem?\ – user3222991 Mar 14 '14 at 06:58
  • lable text is comes dynamic or static lable – Kirit Modi Mar 14 '14 at 07:01
  • static label brother. i have text very length like "sachin tendulkar is world number one bats man.he is doing 100 centuries.In his carrer having double century also". this is my text.i want to display this text in single line – user3222991 Mar 14 '14 at 07:04
  • only that label is dispaly nothing else. ok – Kirit Modi Mar 14 '14 at 07:07
  • i cant understand your above reply.But in below side i want to make audio player view like play and pause and forward and backbard buttons and progress bar and volume slider buttons – user3222991 Mar 14 '14 at 07:09
  • but in audioPlayer the lable may be different when you change another song – Kirit Modi Mar 14 '14 at 07:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49716/discussion-between-kirit-modi-and-user3222991) – Kirit Modi Mar 14 '14 at 07:11
  • For those following this post: the 'dots' issue is because the text frame isn't long enough. It's the 3rd value in the CGRectMake call - 200 in the example. So increase the value based on the total # of characters you have - trial and error/repeat until you get the right size. – user3741598 Nov 11 '21 at 00:52
3
 UILabel*label=[[UILabel alloc]init];
    label.text=@"Song Name";
    label.frame=CGRectMake(321, 20, 300, 30);
    [self.view addSubview:label];
    [UIView beginAnimations:@"" context:nil];
    [UIView setAnimationDuration:20.0];
    label.frame=CGRectMake(0, 20, 300, 30);

    [UIView commitAnimations];

Or you can try this out if you want to repeat the scrolling of the text

UILabel*label=[[UILabel alloc]init];
    label.text=@"Song Name";
    label.frame=CGRectMake(321, 20, 300, 30);
    [self.view addSubview:label];
    [UIView animateWithDuration:5.0 delay:0.0 options: UIViewAnimationOptionRepeat
                     animations:^{
                         label.frame=CGRectMake(-100, 20, 300, 30);
                     }completion:^(BOOL finished){
                     }];
Swati Gupta
  • 354
  • 1
  • 9
  • For those following this post: this is still working in Xcode 13 - seems cleaner if you're not going to be calling the method over and over. – user3741598 Nov 11 '21 at 00:54
1

//Call this method where you need this. // and in this method write this 4 lines of code

[self Message:@"test"];

- (void)Message:(NSString *)messageString
{
UILabel *label = [[UILabel alloc] initWithFrame:(CGRectMake(321, 20, 300, 30))];
label.text = messageString;
label.backgroundColor = [UIColor clearColor];
[self.view addSubview:label];
[UIView beginAnimations:@"test" context:nil];
[UIView setAnimationDuration:3];
[UIView setAnimationDidStopSelector:@selector(Message:)];
[UIView setAnimationDelegate:self];

label.frame = CGRectMake(-100, 20, 300, 30);
[UIView commitAnimations];
}

enter code here

It works..

NIKETA SETH
  • 39
  • 1
  • 3
0

you could try this one

[UIView animateWithDuration:15.0f animations:^{
        Moving_Cloud.frame = CGRectMake(320.0f, 30.0f, Moving_Cloud.frame.size.width, Moving_Cloud.frame.size.height);
    }
                     completion:^(BOOL finished){
                     }];

here " Moving_Cloud " is my image view so likewise you can try for your label.

Agent Chocks.
  • 1,312
  • 8
  • 19
0

Yo can use UIView Animation Blocks for that

 [UIView animateWithDuration:5.0f delay:0.0f options:UIViewAnimationOptionTransitionNone animations:^{
            yourLabel.center = CGPointMake(0, yourLabel.center.y);
        } completion:NULL ];

And if you want something like autoreverses

[UIView animateWithDuration:5.0f delay:0.0f options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionBeginFromCurrentState animations:^{
            yourLabel.center = CGPointMake(0, yourLabel.center.y);
        } completion:NULL ];
Himanshu Joshi
  • 3,391
  • 1
  • 19
  • 32
0

Use the below method

- (void)marqueeLabel:(UILabel *)label
{
    __block UILabel *labelToBeMarqueed = label;
    __block CGRect labelFrame = labelToBeMarqueed.bounds;
    labelFrame.origin.x = [UIScreen mainScreen].bounds.size.width;
    labelToBeMarqueed.frame = labelFrame;
    [UIView animateWithDuration:2.0f
                 animations:^{
                     labelFrame.origin.x = -[UIScreen mainScreen].bounds.size.width;
                     labelToBeMarqueed.frame = labelFrame;
                 } completion:^(BOOL finished) {
                     [self marqueeLabel:label];
                 }];
}

Pass the label that you want to move from right to left to this method. Add a condition to stop the animation as this method loops continuously. You can change the animation duration as required.

Adithya
  • 4,545
  • 3
  • 25
  • 28
  • @Aditya thanks for your response.But i have small doubt.where i can specify my moving text? like i want to scrollong this text "iphone is one of the good smartphone".I want to scrool the text. how to assign the text? – user3222991 Mar 14 '14 at 06:07
  • @user3222991 Create a label, add the text you want to display, position the label in your view and then pass that label as argument to this method. – Adithya Mar 14 '14 at 06:09
0

IN Swift you can implement it like this

override func viewDidLoad() {
    super.viewDidLoad()

    marqueeLabel = UILabel(frame: CGRectMake(320, 100, 400, 60))
    marqueeLabel.text = "Your music title here"
    self.view.addSubview(marqueeLabel)

    UIView.animateWithDuration(10.0, delay: 0.0, options: [.Repeat], animations: { () -> Void in
        self.marqueeLabel.frame = CGRectMake(-320, 100, 400, 60)
        }, completion: { (finished: Bool) -> Void in
            self.marqueeLabel.frame = CGRectMake(320, 100, 400, 60)
    });
}
rkj
  • 8,067
  • 2
  • 27
  • 33
0

Add View with background colour.

UIView *animatedLblView = [[UIView alloc]initWithFrame:CGRectMake(0, 75, self.view.frame.size.width, 30)];
animatedLblView.backgroundColor = [UIColor colorWithRed:0.00 green:0.34 blue:0.61 alpha:1.0];
[self.view addSubview:animatedLblView];
//Our animated component(UIButton)
_animatingButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width+100, 30)];
[_animatingButton setTitle:@"Upload documents, please contact 1234567890" forState:UIControlStateNormal];
[animatedLblView addSubview:_animatingButton];
//Timer
[NSTimer scheduledTimerWithTimeInterval:1.0
                                 target:self
                               selector:@selector(LabelAnimation)
                               userInfo:nil
                                repeats:nil];

-(void)LabelAnimation {
    //Set animation
    [UIView animateWithDuration:10.0f delay:0.0f options:UIViewAnimationOptionTransitionNone animations:^{
    _animatingButton.frame = CGRectMake(-(self.view.frame.size.width+100), 0, self.view.frame.size.width+100, 30);
    } completion:^(BOOL finished) {
     _animatingButton.frame = CGRectMake(self.view.frame.size.width, 0, 0, 30);
        // Call the same function
        [self LabelAnimation];
     }];
}
Naresh
  • 16,698
  • 6
  • 112
  • 113
0

for Swift just run this func:

 func startMarqueeLabelAnimation() {

    DispatchQueue.main.async(execute: {

        UIView.animate(withDuration: 20.0, delay: 1, options: ([.curveLinear, .repeat]), animations: {() -> Void in
            self.marqueeLabel.center = CGPoint(x: 0 - self.marqueeLabel.bounds.size.width / 2, y: self.marqueeLabel.center.y)

        }, completion:  nil)

    })
}