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

4

我希望在我的应用程序中展示一些文本,就像移动的文本(从右向左带有动画效果滚动)。如何通过编程实现?

我使用了UIViewcontroller。我正在开发AVAudioplayer。因此,文本将从右侧移动到左侧的UIViewController顶部。


你可以分享一下截图吗?因为这样可以更清晰地理解。 - TamilKing
1
你想要一个跑马灯效果吗?如果是的话,可以看一下这个链接:https://dev59.com/BnLYa4cB1Zd3GeqPbsg7 - carloabelli
@hey Tamilking,感谢您的回复。但是我没有任何屏幕截图。但是您是否看过一些新闻网站,新闻从右向左滚动?我需要实现这种功能。 - user3222991
9个回答

14

首先,在视图中添加一个标签,并将其框架设置为以下内容以使其在视图外。

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

}

现在,在 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);
     }];

}

以下是输出结果。

这里输入图片描述


它的工作非常好。但是我有一个很长的字符串,像“萨钦·滕度卡尔是世界排名第一的击球手”。这是我的文本。如果我将文本粘贴到标签中,我会得到省略号。所以请帮助我如何将文本固定在单行中。 - user3222991
我已经给了320,但仍然显示点。那么如何解决这个问题? - user3222991
静态标签兄弟。我有一个非常长的文本,像“萨钦·滕度卡是世界排名第一的击球手。他打了100个世纪。在他的职业生涯中也有双百分之一”。这是我的文本。我想在单行中显示这个文本。 - user3222991
但是在音频播放器中,当您切换另一首歌曲时,标签可能会有所不同。 - Kirit Modi
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/49716/discussion-between-kirit-modi-and-user3222991 - Kirit Modi
显示剩余5条评论

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

如果您想重复滚动文本,则可以尝试以下方法

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

对于关注此帖子的人:在Xcode 13中仍然有效 - 如果您不会一遍又一遍地调用该方法,似乎更加简洁。 - user3741598

1
//在需要的位置调用此方法。 //然后在该方法中编写以下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

它有效。


0

添加带有背景颜色的视图。

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

0

对于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)

    })
}

0

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

0

你可以试试这个

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

这里的 "Moving_Cloud" 是我的图像视图,同样地,您可以尝试用标签。


0
你可以使用 UIViewAnimation 块来实现。
 [UIView animateWithDuration:5.0f delay:0.0f options:UIViewAnimationOptionTransitionNone animations:^{
            yourLabel.center = CGPointMake(0, yourLabel.center.y);
        } completion:NULL ];

如果你想要类似于autoreverses的东西

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

0
请使用以下方法:
- (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];
                 }];
}

将要从右到左移动的标签传递给此方法。添加一个条件来停止动画,因为此方法会不断循环。您可以根据需要更改动画持续时间。

@Aditya,感谢您的回复。但我有一个小疑问。我在哪里可以指定我的滚动文本?比如我想滚动这个文本“iPhone是一款不错的智能手机”。我想要滚动这个文本。如何分配这个文本? - user3222991
@user3222991 创建一个标签,添加你想要显示的文本,将标签定位在你的视图中,然后将该标签作为参数传递给这个方法。 - Adithya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接