UILabel 刷新 [英] UILabel Refresh

查看:50
本文介绍了UILabel 刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经研究过这个,但我仍然不太清楚如何去做.我有一个 UILabel.我想写出一个词……由后者来信.它遍历每个字母,然后在最后显示整个单词.一开始我以为只是因为速度太快,所以我就用这样的东西延迟了.

I have looked into this, but I’m still not quite clear how to do it. I have a UILabel. I would like to write out a word... letter by latter. It goes through each letter, then at the end the entire word shows up. At first I thought it was just because it was too fast, so I put in a delay with something like this.

    typedWord.text = [typedWord.text stringByAppendingString:@"C"];
    future = [NSDate dateWithTimeIntervalSinceNow: letterPause ];
    [NSThread sleepUntilDate:future];

然后我读到可能是因为它在函数调用完成之前不会刷新.建议是使用选择器调用.我不知道如何做到这一点.有什么建议么?

Then I read that it may be because it doesn’t refresh until the function call is finished. The suggestion was to use a selector call. I’m not sure how to do this. Any suggestions?

推荐答案

您正在休眠主线程,因此 UI 在休眠结束之前不会刷新.

You are sleeping the main thread, so the UI is not getting refreshed until the end of the sleep.

您需要实现如下内容:

// This code is in your view controller somewhere, where you initially decide to update the label. typedWord is the outlet to your UILabel.
NSString *newText = @"Hello";
typedWord.text = @"";
[self performSelector:@selector(updateLabel:) withObject:newText afterDelay:0.2];

这调用:

// This is a separate method in your view controller, so typedWord still refers to the label outlet.
-(void)updateLabel:(NSString*)newText 
{
    // Get the first character of the passed in string
    NSString *firstCharacter = [newText substringToIndex:1];

    // Add this to whatever the current label text is
    typedWord.text = [NSString stringWithFormat:@"%@%@",typedWord.text,firstCharacter];

    // Trim off the first character
    NSString *remainingCharacters = [newText stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:@""];

    //If we still have characters left, do the loop again.
    if (![remainingCharacters isEqualToString:@""])
        [self performSelector:@selector(updateLabel:) withObject:remainingCharacters afterDelay:0.2];
}

这篇关于UILabel 刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆