UILabel的字母动画?

vig*_*mar 23 animation uilabel ios

有没有办法为显示的文本设置动画UILabel.我希望它按字符显示文本值.

帮帮我这个人

And*_* G. 54

更新2018年,Swift 4.1:

extension UILabel {

    func animate(newText: String, characterDelay: TimeInterval) {

        DispatchQueue.main.async {

            self.text = ""

            for (index, character) in newText.enumerated() {
                DispatchQueue.main.asyncAfter(deadline: .now() + characterDelay * Double(index)) {
                    self.text?.append(character)
                }
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

调用它很简单且线程安全:

myLabel.animate(newText: myLabel.text ?? "May the source be with you", characterDelay: 0.3)
Run Code Online (Sandbox Code Playgroud)

@objC,2012:

试试这个原型功能:

- (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];
    }
}
Run Code Online (Sandbox Code Playgroud)

并以这种方式称呼它:

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

  • 它工作正常..我试了很多次. (2认同)
  • 这种方法的问题是,如果你在一行的末尾开始写一个单词,然后它需要转移到第二行(即,如果你添加了足够多的字符)。这将导致单词的一半出现在第一行,然后一旦意识到该单词就会猛地跳到下一行。 (2认同)
  • 如果我不对,请纠正我,这种方法使用 CPU 进行动画处理。但据我所知,这不是推荐的方式。有谁知道核心动画方式(基本上使用 GPU 制作动画)? (2认同)

Ada*_*ite 9

这里是@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)
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)


小智 7

这可能会更好.

- (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]];
}
Run Code Online (Sandbox Code Playgroud)