IOS中label的文字一个个动态出现

程序很简单,关键是涉及多线程问题以及线程休眠问题

下面看下代码:

#import "ViewController.h"


@interface ViewController ()

{

    UILabel *_label;

    NSString *_str;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

   

    self.view.backgroundColor = [UIColor whiteColor];

    _label = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 375-100, 300)];

    _label.numberOfLines = 0;

    _label.font = [UIFont systemFontOfSize:16];

    _label.backgroundColor = [UIColor purpleColor];

    _label.textColor = [UIColor blackColor];

    [self.view addSubview:_label];

    //开启线程

    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(animationLabel) object:nil];

    [thread start];

    //初始化字符串,为我们最后显示内容

    _str = @"感谢对本文的收看,希望能帮到你";

    

}

//线程的代码体,执行休眠

-(void)animationLabel

{

    for (int i=0; i<_str.length; i++) {

        //每次给Label显示的文字是从最前边到i+1个

        [self performSelectorOnMainThread:@selector(refreshUI:) withObject:[_str substringWithRange:NSMakeRange(0, i+1)]  waitUntilDone:YES];

        [NSThread sleepForTimeInterval:0.3];

    }

    

}

//执行主线程修改UI

-(void)refreshUI:(NSString *)contentStr

{

    _label.text = contentStr;

}


@end



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值