iOS中 噴槍打字動畫的實現
實現原理比較簡單,這裡不做過多介紹.
#import ViewController.h@interface ViewController ()@property (weak, nonatomic) IBOutlet UILabel *titleLabel;@property (copy , nonatomic)NSString *contentStr;@end
- (void)viewDidLoad{ [super viewDidLoad]; NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(animationLabel) object:nil]; [thread start]; self.contentStr = @人生最寶貴的是生命,生命屬於人只有一次。一個人的生命應當這樣度過:當他回憶往事的時候,他不致因虛度年華而悔恨,也不致因碌碌無為而羞愧;在臨死的時候,他能夠說:“我的整個生命和全部精力,都已獻給世界上最壯麗的事業——為人類的解放而鬥爭。”; }
- (void)animationLabel{ for (NSInteger i = 0; i < self.contentStr.length; i++) { [self performSelectorOnMainThread:@selector(refreshUIWithContentStr:) withObject:[self.contentStr substringWithRange:NSMakeRange(0, i+1)] waitUntilDone:YES]; [NSThread sleepForTimeInterval:0.3]; }}
- (void)refreshUIWithContentStr:(NSString *)contentStr{ self.titleLabel.text = contentStr;}
最終效果: