How to move text from right to left in iOS programmatically

后端 未结 9 1745
野趣味
野趣味 2020-12-28 11:33

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

相关标签:
9条回答
  • 2020-12-28 11:33

    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

    0 讨论(0)
  • 2020-12-28 11:35

    //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..

    0 讨论(0)
  • 2020-12-28 11:39

    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)
        });
    }
    
    0 讨论(0)
  • 2020-12-28 11:45
     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){
                         }];
    
    0 讨论(0)
  • 2020-12-28 11:50

    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.

    0 讨论(0)
  • 2020-12-28 11:50

    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 ];
    
    0 讨论(0)
提交回复
热议问题