简体   繁体   中英

how to count down time?

i want to count down time from 10 to 0. and em doing this by this code.. but its not working fine

-(void)elapsedTime

{

    static int i = 11;

    UILabel *label;

    label = [[UILabel alloc] initWithFrame:CGRectMake(850, 27, 50, 50)];

    label.textColor = [UIColor redColor];

    label.font = [UIFont boldSystemFontOfSize:25.0f];

    label.backgroundColor=[UIColor blackColor];

    label.text = [NSString stringWithFormat:@"%i",i];

    [self.view addSubview:label]; 
    i--;
    if(i<0)
    {
        [aTimer invalidate];
        aTimer = nil;
    }
    [label sendSubviewToBack:self.view];
}

You should use NSTimer for this puropose like this.

In .h file put

{
NSTimer *timer
}

In .m file put this

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

Now add a function, it will be called every 1 second. You can update your label in this function.

-(void)updateLabel {
    if (timeCount==0) {
        [timer invalidate];

    } else {
        timeCount = timeCount-1;
        label.text = [NSString stringWithFormat:@"%d",timeCount];
    }
}

Hope this helps.

Use below code for your app. and call addlabel from viewDidload and define label and i in .h file.

-(void)addlabel{

i=10;

label = [[UILabel alloc] initWithFrame:CGRectMake(150, 27, 50, 50)];

label.textColor = [UIColor redColor];

label.font = [UIFont boldSystemFontOfSize:25.0f];

label.backgroundColor=[UIColor blackColor];

label.text = [NSString stringWithFormat:@"%i",i];

[self.view addSubview:label];

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

}

-(void)elapsedTime{

i--;
label.text = [NSString stringWithFormat:@"%i",i];
if(i<1)
{
    [aTimer invalidate];
    aTimer = nil;
}

}

try this

.h

            NSTimer* time;
            int count;

.m file

    viedidload
    count=10;

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

    -(void)timeShedule {
        label.text=[NSString stringWithFormat:@"%d",count];
         count--;
        if (count==0) {
           [time invalidate];
            time=nil;
       }
    }

in .h

UILabel *label;

in .m

- (void)viewDidLoad
{
    label = [[UILabel alloc] initWithFrame:CGRectMake(850, 27, 50, 50)];
    label.textColor = [UIColor redColor];
    label.font = [UIFont boldSystemFontOfSize:25.0f];
    label.backgroundColor=[UIColor blackColor];
    label.text = @"10";
    [self.view addSubview:label]; 

    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(elapsedTime:) userInfo:nil repeats:YES];
}

- (void)elapsedTime:(NSTimer *)timer
{
    int currentValue = [label.text intValue];
    currentValue--;
    label.text = [NSString stringWithFormat:@"%i",currentValue];

    if (currentValue == 0)
    {
        [self.view sendSubviewToBack:label];
        [timer invalidate];
        timer = nil;
    }
}

If you creating some counter application like http://download.cnet.com/Work-Time-Counter/3000-2124_4-75903089.html

you need to use NSDate like follow:

-(void) start {
   // Save start time
   self.started = [[NSDate alloc] init];
   self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                              target:self
                                            selector:@selector(updateCallback)
                                            userInfo:nil
                                             repeats:YES];
}

-(void)updateCallback {
   // Count time since start and notify UI
   [self.callback counterTick:[ self.started timeIntervalSinceNow ]];  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM