创建一个随时间推移计数的标签,iOS

时间:2015-03-16 06:46:55

标签: ios multithreading count grand-central-dispatch nstimer

我正在尝试创建一个标签(或任何UI元素),该标签从一个数字开始并计数,同时不会减慢所有其他进程的速度。

我不确定如何使用Multithreading / GCD / NSTimer最适合使用iOS。有什么建议?这是我目前的代码:

    -(void)countup:(UIBarButtonItem *)label from:(int)startNumber to:(int)endNumber{
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //         double timeInterval = 5; //5 seconds from start to stop
    //         int diff = endNumber - startNumber;
    //         double t = timeInterval/diff;
             [NSThread sleepForTimeInterval: 0.01];

             if(diff==0){
                 return;
             }
             if(diff>0){
                 label.title= [NSString stringWithFormat:@"%d", startNumber + 1];
                 [self countup:label from:startNumber+1 to:endNumber];
             }
             if(diff<0){
                 label.title = [NSString stringWithFormat:@"%d", startNumber - 1];
                 [self countup:label from:startNumber-1 to:endNumber];
             }
        });
    }

不幸的是,这只是从我拥有的第一个号码开始,等待一段时间,然后让我到最后一个号码。

I've also tried the following with little luck: 

-(void)countup:(UIBarButtonItem *)label from:(int)startNumber to:(int)endNumber{
//         double timeInterval = 5; //5 seconds from start to stop
         int diff = endNumber - startNumber;
//         double t = timeInterval/diff;

         if(diff==0){
             return;
         }
         if(diff>0){
             label.title= [NSString stringWithFormat:@"%d", startNumber + 1];
             [self countup:label from:startNumber+1 to:endNumber];
             [NSTimer scheduledTimerWithTimeInterval:0.1
                                            target:self
                                            selector:@selector(callCountUp:)
                                            userInfo:[NSDictionary dictionaryWithObjectsAndKeys:label,
                                                      @"value1", startNumber, @"value2", endNumber, @"value3",nil]
                                            repeats:YES];
         }
         if(diff<0){
             label.title = [NSString stringWithFormat:@"%d", startNumber - 1];
             [self countup:label from:startNumber-1 to:endNumber];
         }
}

- (void)callCountUp:(NSTimer *)theTimer {
    UIBarButtonItem *value1 = [[theTimer userInfo] objectForKey:@"value1"];
    int value2 = (int)[[theTimer userInfo] objectForKey:@"value2"];
    int value3 = (int)[[theTimer userInfo] objectForKey:@"value3"];
    [self countup: value1 from:value2 to:value3];
}

2 个答案:

答案 0 :(得分:0)

您必须更新主线程中屏幕上显示的UI元素,因此您更改标签文本的每一行都必须包含在

dispatch_async(dispatch_get_main_queue){
    // change labels text
}

还有更好的方法可以做你想要的,你应该查看NSTimer,例如你可以这样做

timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                 target:self
                               selector:@selector(updateTime)
                               userInfo:nil
                                repeats:YES];

这会每秒调用updateTime函数,直到你停止它

答案 1 :(得分:0)

以下代码最终为我工作:

-(void)countup:(UIBarButtonItem *)label from:(int)startNumber to:(int)endNumber{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        double diff = endNumber - startNumber;
        double t = 5;
        t = t/((diff*diff)+5);
        if(t<0){
            t=t*-1;
        }
        [NSThread sleepForTimeInterval: t];

        if(diff==0){
            return;
        }
        if(diff>0){
            dispatch_async(dispatch_get_main_queue(), ^{
                [UIView setAnimationsEnabled:NO];
                label.title= [NSString stringWithFormat:@"%d", startNumber + 1];
                [UIView setAnimationsEnabled:YES];
            });
            [self countup:label from:startNumber+1 to:endNumber];
        }
        if(diff<0){
            dispatch_async(dispatch_get_main_queue(), ^{
                label.title= [NSString stringWithFormat:@"%d", startNumber - 1];
            });
            [self countup:label from:startNumber-1 to:endNumber];
        }
    });
}