如何在3个值之间为uilabel文本设置动画

Sab*_*ath 3 animation objective-c uilabel ios

我想要的是重复地在3个值之间动画我的UILabel文本.我要设置的文字是"下载","下载..","下载...",然后重复.有了这个动画,我想让我的用户知道正在完成下载并且应用程序没有堆叠.我一直在谷歌搜索一段时间,但没有找到解决方案.

任何人都可以给我一些参考吗?提前致谢.

Dil*_*lip 6

使用NStimer更改文本

.h文件中

@interface ViewController : UIViewController
{
    NSTimer * timer;
    UILabel * downloadingLbl;
}
Run Code Online (Sandbox Code Playgroud)

并在.m档案中

- (void)viewDidLoad
{
    [super viewDidLoad];

    downloadingLbl.text = @"Downloading.";
    if (!timer) {
        timer = [NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(onTick:) userInfo:nil repeats:YES];
    }
}

-(void)onTick:(NSTimer*)timer
{
    NSLog(@"Tick...");

    if ([downloadingLbl.text isEqualToString:@"Downloading."]) {
        downloadingLbl.text = @"Downloading..";
    } else if ([downloadingLbl.text isEqualToString:@"Downloading.."]) {
        downloadingLbl.text = @"Downloading...";
    } else if ([downloadingLbl.text isEqualToString:@"Downloading..."]) {
        downloadingLbl.text = @"Downloading.";
    }
}
Run Code Online (Sandbox Code Playgroud)

当你的下载完成,使计时器失效.

[timer invalidate];
Run Code Online (Sandbox Code Playgroud)

  • @Sabbath我认为这是更合适的答案bcoz你不必为进程维护另一个变量. (4认同)

Dha*_*nia 5

为此你必须像这样安排计时器:

int count = 1;
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1
                       target:self 
                       selector:@selector(updateLabel) 
                       userInfo:nil 
                       repeats:YES];
Run Code Online (Sandbox Code Playgroud)

现在,下面的方法将在每个(几秒)时间内被调用

- (void) updateLabel {
    if(count == 1)
    {
    label.text = @"Downloading."

    count = 2;
    }
    else if(count == 2)
    {
    label.text = @"Downloading.."

    count = 3;
    }
    else if(count == 3)
    {
    label.text = @"Downloading..."

    count = 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

每当你想停止更新时,在你想要停止下载的场景中:

[timer invalidate];
Run Code Online (Sandbox Code Playgroud)