如何自动滚动标签中的文本

时间:2014-07-24 09:48:49

标签: objective-c ios7 uiscrollview nstimer

我有一个数组,其数据使用UILabel显示在scrollview上。我可以左右滚动。但我想添加一个功能,即在scrollview上自动滚动标签文本。这意味着即使我不滚动,我希望它自己移动。 我的滚动代码如下:

- (void)viewDidLoad
{
    [super viewDidLoad];

    _scrollView.contentInset=UIEdgeInsetsMake(0, 300, 0, 300);

    NSMutableArray *items=[[NSMutableArray alloc] initWithObjects:@"A walk to remember",@"The Last song",@"The Notebook",@"Dear John",@"The Longest Ride",nil];

    CGFloat y = 10;
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(0,y,800, 25)];

    for(int i=0;i<[items count];i++){
          y+=20;        
         NSString* lab=[items objectAtIndex:i];
        _label.backgroundColor=[UIColor clearColor];
        lab=[items componentsJoinedByString:@" | "];
        NSLog(@"label:%@",lab);
        [self.label setLineBreakMode:NSLineBreakByWordWrapping];
        [self.label setText:lab];     
    }   

    NSTimer *timer = [NSTimer timerWithTimeInterval:1
                                             target:self
                                           selector:@selector(timer)
                                           userInfo:nil
                                            repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; 

    //[_scrollView scrollRectToVisible:NSMakeRange(0,0,0.0) animated:YES]; 
    [_scrollView addSubview:_label];
    [UIView commitAnimations];    

    _scrollView.contentSize=CGSizeMake(_scrollView.frame.size.width*[items count], _scrollView.frame.size.height);   
    }

计时器代码:

-(void)timer {

[self.label setText:[NSString stringWithFormat:@" %@", self.label.text]];

}

请帮助我如何使标签文字水平移动。谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

如果可能的话,这是实现滚动的一种相当可怕的方式!您通过在字符串的左侧插入增量空格来滚动。有一种更简单的方法可以做到这一点!

  • 保留将UIL用户添加到滚动视图的代码
  • 摆脱NSTimer的东西
  • 添加基本的UIView动画,为scrollview的内容偏移设置动画。

这样的事情:

[UIView animateWithDuration:0.3f delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{
        _scrollview.contentOffset = CGPointMake(-50.0f, 0.0f);
    } completion:nil];

这将使整个滚动视图在0.3秒内向左滑动50个点。您可以使用这些设置。

顺便说一下,通常你会在viewDidLoad中创建UILabel(就像你拥有它一样)但是把动画放在viewWillAppear或viewDidAppear

再看看你的代码。我以为你在你的uiscrollview中添加了多个UILabel,但实际上你只添加了一个带有长文本字符串的UILabel。

您可以简化代码以设置标签,然后根据下面的内容实现滚动动画。

NSMutableArray *items=[[NSMutableArray alloc] initWithObjects:@"A walk to remember",@"The Last song",@"The Notebook",@"Dear John",@"The Longest Ride",nil];

CGFloat y = 10;
self.label = [[UILabel alloc]initWithFrame:CGRectMake(0,y,800, 25)];
[self.label setLineBreakMode:NSLineBreakByWordWrapping];
[self.label setBackgroundColor: [UIColor clearColor]];
[self.label setText: [items componentsJoinedByString:@" | "]];

修改

关于如何显示整行滚动然后重复的问题,这里有几个步骤。

  • 使UILabel足够宽以将所有文本放在一行上 - 即增加宽度,使其足够大以适合所有文本。您可以通过反复试验或使用(CGSize)sizeThatFits:(CGSize)size
  • 来执行此操作

获得大小。您调用CGSize size = self.label sizeThatFits:CGSizeMake(CGFloatMax, self.label.frame.size.height)来获取大小,然后将其应用于标签的框架:

CGRect frame = self.label.frame;
frame.size.width = size.width; //calculated above with sizeThatFits
self.label.frame = frame; //update the frame
  • 现在滚动滚动视图的全宽。因此animateWithDuration代码将变为:

    _scrollview.contentOffset = CGPointMake(-size.width,0.0f);

  • 最后一步是如何在它结束后重新启动它。不确定你想要什么效果?它应该跳回到开始吗? (如果是这样,将contentOffset x设置为零),还是继续滚动? (如果是这样,请使用一个技巧,比如在第一个结尾处添加第二个UILabel。

顺便说一句,如果你所追求的是一个简单的滚动文本,就像网页上的横幅一样,那么我建议你根本不用滚动视图。而是将UILabel添加到self.view,然后直接为UILabel的帧原点设置动画。