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

我想在我的应用程序中显示一些文本,如移动文本(从右向左滚动animation)。 我不知道如何做这个编程?

我带了UIViewcontroller 。我正在开发UIViewcontrollerAVAudioplayer ,在UIViewController的顶端文本将从右向左移动。

首先,您在视图中标注标签,并将其框架设置为如下所示。

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

现在,该标签给出的animation如下在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); }]; } 

输出在下面。

在这里输入图像说明

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

//在需要的地方调用这个方法。 //在这个方法中写这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 

有用..

你可以试试这个

 [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”是我的图片视图,所以你也可以尝试一下你的标签。

哟可以使用UIView Animation

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

使用下面的方法

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

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

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