使用NSThread让字体一个一个弹出

来源:互联网 发布:什么软件可以四连拍 编辑:程序博客网 时间:2024/05/17 07:06

 在开发中,经常可见字一个一个的弹出的情形,下面就使用NSThread来实现这种功能。

#import "ViewController.h"


@interface ViewController ()

@property (weak, nonatomic) IBOutletUILabel *titleLabel;

@property (copy , nonatomic)NSString *contentStr;


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];


    NSThread *thread = [[NSThreadalloc]initWithTarget:selfselector:@selector(animation)object:nil];

    [thread start];

    self.contentStr = @"我需要三件东西:爱情友谊和图书。然而这三者之间何其相通!炽热的爱情可以充实图书的内容,图书又是人们最忠实的朋友";

}

- (void)animation

{

    for (NSInteger i =0; i <self.contentStr.length; i++)

    {

        [selfperformSelectorOnMainThread:@selector(refreshUIWithContentStr:)withObject:[self.contentStrsubstringWithRange:NSMakeRange(0, i+1)]waitUntilDone:YES];

        [NSThreadsleepForTimeInterval:0.3];

    }

}

- (void)refreshUIWithContentStr:(NSString *)contentStr

{

    self.titleLabel.text = contentStr;

}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end



1 0