字母UILabelanimation?

有没有办法让UILabel显示的文字动起来? 我希望它显示字符的文字值。

帮助我与这个人

2017年更新,Swift 3:

 extension UILabel { func animate(newText: String, characterDelay: TimeInterval) { DispatchQueue.main.async { self.text = "" for (index, character) in newText.characters.enumerated() { DispatchQueue.main.asyncAfter(deadline: .now() + characterDelay * Double(index)) { self.text?.append(character) } } } } } 

称之为简单线程安全:

 myLabel.animate(newText: myLabel.text ?? "May the source be with you", characterDelay: 0.3) 

@objC,2012:

试试这个原型function:

 - (void)animateLabelShowText:(NSString*)newText characterDelay:(NSTimeInterval)delay { [self.myLabel setText:@""]; for (int i=0; i<newText.length; i++) { dispatch_async(dispatch_get_main_queue(), ^{ [self.myLabel setText:[NSString stringWithFormat:@"%@%C", self.myLabel.text, [newText characterAtIndex:i]]]; }); [NSThread sleepForTimeInterval:delay]; } } 

并以这种方式称呼它:

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ [self animateLabelShowText:@"Hello Vignesh Kumar!" characterDelay:0.5]; }); 

@Andrei G.的答案是Swift扩展:

 extension UILabel { func setTextWithTypeAnimation(typedText: String, characterInterval: NSTimeInterval = 0.25) { text = "" dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0)) { for character in typedText.characters { dispatch_async(dispatch_get_main_queue()) { self.text = self.text! + String(character) } NSThread.sleepForTimeInterval(characterInterval) } } } } 

这可能会更好。

 - (void)viewDidLoad { [super viewDidLoad]; NSString *string =@"Risa Kasumi & Yuma Asami"; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:string forKey:@"string"]; [dict setObject:@0 forKey:@"currentCount"]; NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(typingLabel:) userInfo:dict repeats:YES]; [timer fire]; } -(void)typingLabel:(NSTimer*)theTimer { NSString *theString = [theTimer.userInfo objectForKey:@"string"]; int currentCount = [[theTimer.userInfo objectForKey:@"currentCount"] intValue]; currentCount ++; NSLog(@"%@", [theString substringToIndex:currentCount]); [theTimer.userInfo setObject:[NSNumber numberWithInt:currentCount] forKey:@"currentCount"]; if (currentCount > theString.length-1) { [theTimer invalidate]; } [self.label setText:[theString substringToIndex:currentCount]]; } 

我已经写了一个演示,你可以使用它,它支持ios 3.2及以上

在你的.m文件中

 - (void)displayLabelText { i--; if(i<0) { [timer invalidate]; } else { [label setText:[NSString stringWithFormat:@"%@",[text substringToIndex:(text.length-i-1)]]]; } } - (void)viewDidLoad { [super viewDidLoad]; label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 60)]; [label setBackgroundColor:[UIColor redColor]]; text = @"12345678"; [label setText:text]; [self.view addSubview:label]; i=label.text.length; timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(displayLabelText) userInfo:nil repeats:YES]; [timer fire]; } 

在你的.h文件中

 @interface labeltextTestViewController : UIViewController { UILabel *label; NSTimer *timer; NSInteger i; NSString *text; } 

随着演示,我认为你可以做你的情况,用一点点改变代码看起来非常非常丑,因为我必须去吃晚饭,你可以专注于它。

斯威夫特3,还记得Andrei G.的概念。

 extension UILabel{ func setTextWithTypeAnimation(typedText: String, characterInterval: TimeInterval = 0.25) { text = "" DispatchQueue.global(qos: .userInteractive).async { for character in typedText.characters { DispatchQueue.main.async { self.text = self.text! + String(character) } Thread.sleep(forTimeInterval: characterInterval) } } } } 

我已经编写了一个专门用于此用例的轻量级库,名为CLTypingLabel ,可在GitHub上获得。

它是有效的,安全的,不睡任何线程。 它还提供pausecontinue界面。 随时调用它,它不会中断。

安装CocoaPods之后,将下面的内容添加到您的Podfile中以使用它:

 pod 'CLTypingLabel' 

示例代码

将标签的类从UILabel更改为CLTypingLabel; 在这里输入图像说明

 @IBOutlet weak var myTypeWriterLabel: CLTypingLabel! 

在运行时,设置标签的文本会自动触发animation:

 myTypeWriterLabel.text = "This is a demo of typing label animation..." 

您可以自定义每个字符之间的时间间隔:

 myTypeWriterLabel.charInterval = 0.08 //optional, default is 0.1 

您可以随时暂停inputanimation:

 myTypeWriterLabel.pauseTyping() //this will pause the typing animation myTypeWriterLabel.continueTyping() //this will continue paused typing animation 

还有一个样本项目,来自cocoapods

在UILabel中没有默认的行为来做到这一点,你可以根据定时器,一次添加一个字母

OK根据第一个答案,这是我写的

  import Foundation var stopAnimation = false extension UILabel { func letterAnimation(newText:NSString?,completion:(finished:Bool) -> Void){ self.text = "" if !stopAnimation { dispatch_async(dispatch_queue_create("backroundQ", nil)){ if var text = newText { text = (text as String) + " " for(var i = 0; i < text.length;i++){ if stopAnimation { break } dispatch_async(dispatch_get_main_queue()) { let range = NSMakeRange(0,i) self.text = text.substringWithRange(range) } NSThread.sleepForTimeInterval(0.05) } completion(finished:true) } } self.text = newText as? String } } } 

我知道这答案为时已晚,但以防在UITextView中查找inputanimation的人员。 我为Swift 4编写了一个小型库Github 。你可以在animation结束时设置callback。

 @IBOutlet weak var textview:TypingLetterUITextView! textview.typeText(message, typingSpeedPerChar: 0.1, completeCallback:{ // complete action after finished typing } 

此外,我有UILabel扩展键入animation。

 label.typeText(message, typingSpeedPerChar: 0.1, didResetContent = true, completeCallback:{ // complete action after finished typing } 

基于@Adam Waite的答案。 如果有人想使用这个完成closures。

  func setTextWithTypeAnimation(typedText: String, characterInterval: NSTimeInterval = 0.05, completion: () ->()) { text = "" dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0)) { for character in typedText.characters { dispatch_async(dispatch_get_main_queue()) { self.text = self.text! + String(character) } NSThread.sleepForTimeInterval(characterInterval) } dispatch_async(dispatch_get_main_queue()) { completion() } } } 

修改@Adam Waite的代码(很好的工作,顺便说一句)逐字显示的文字:

  func setTextWithWordTypeAnimation(typedText: String, characterInterval: NSTimeInterval = 0.25) { text = "" dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0)) { let wordArray = typedText.componentsSeparatedByString(" ") for word in wordArray { dispatch_async(dispatch_get_main_queue()) { self.text = self.text! + word + " " } NSThread.sleepForTimeInterval(characterInterval) } } 

Andrei提供的答案略有改善,而不是阻止主线程。

 - (void)animateLabelShowText:(NSString*)newText characterDelay:(NSTimeInterval)delay { [super setText:@""]; NSTimeInterval appliedDelay = delay; for (int i=0; i<newText.length; i++) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, appliedDelay * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ [super setText:[NSString stringWithFormat:@"%@%c", self.text, [newText characterAtIndex:i]]]; }); appliedDelay += delay; }