码迷,mamicode.com
首页 > 其他好文 > 详细

用UILabel实现文字滚动播放(跑马灯)效果

时间:2015-10-24 14:14:09      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

- (void)viewDidLoad {
    [super viewDidLoad];
    
  //数据源
    self.messageArray = [NSArray arrayWithObjects:
                         @"1",
                         @"2",
                         @"3",
                         nil];
    self.msgCount = 0;//从第一条开始显示
}
-(void)viewDidAppear:(BOOL)animated {
    //播放第一条并加入Timer设定切换间隔时间
    [self msgChange];
    [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(msgChange) userInfo:nil repeats:YES];
}
- (void)msgChange {

    if (self.msgCount < self.messageArray.count) {
        self.scrollLabel.text = [self.messageArray objectAtIndex:self.msgCount];
        self.msgCount++;
    } else {
        self.scrollLabel.text = @"no message";//此处删除可以改为循环滚动播放
    }
    
    [self.scrollLabel sizeToFit];
    CGRect frame = self.scrollLabel.frame;
    frame.origin.x = [UIScreen mainScreen].bounds.size.width;
    self.scrollLabel.frame = frame;
    
    [UIView beginAnimations:@"scrollLabelTest" context:NULL];
    [UIView setAnimationDuration:5.0f];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationRepeatAutoreverses:NO];
    [UIView setAnimationRepeatCount:0];
    
    frame = self.scrollLabel.frame;
    frame.origin.x = -frame.size.width;
    self.scrollLabel.frame = frame;
    [UIView commitAnimations];
}

 

用UILabel实现文字滚动播放(跑马灯)效果

标签:

原文地址:http://www.cnblogs.com/liuliuliu/p/4906736.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!