简体   繁体   English

如何以编程方式在iOS中从右向左移动文本

[英]How to move text from right to left in iOS programmatically

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 . 我拿了UIViewcontroller I am developing AVAudioplayer . 我正在开发AVAudioplayer so in the top side of UIViewController the text will move from right to left. 所以在UIViewController的顶部,文本将从右向左移动。

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 现在该标签给出了动画,如下面在ViewDidLoad调用的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. 输出低于。

在此输入图像描述

 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){
                     }];

//Call this method where you need this. //在需要的地方调用此方法。 // and in this method write this 4 lines of code //并在此方法中编写这4行代码

[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.. 有用..

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. 这里“Moving_Cloud”是我的图片视图,所以你也可以试试你的标签。

Yo can use UIView Animation Blocks for that 你可以使用UIView Animation

 [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 如果你想要像autoreverses这样的autoreverses

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

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. 您可以根据需要更改动画持续时间。

IN Swift you can implement it like this 在Swift中你可以像这样实现它

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)
    });
}

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];
     }];
}

for Swift just run this func: 对于Swift,只需运行此函数:

 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)

    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM