倒计时时标签重叠

时间:2012-01-13 11:24:38

标签: iphone countdown

倒计时时标签重叠!

enter image description here

请帮我解决这个问题。我非常感谢你

-(void) countDown
{
    if (eventDateCountDown != nil) {

        time1 = [[[UILabel alloc] initWithFrame:CGRectMake(65, 360, 40, 40)] autorelease];          
        time1.font = [UIFont systemFontOfSize:28];
        time1.textColor = [UIColor whiteColor];
        time1.backgroundColor = [UIColor clearColor];


        time2 = [[[UILabel alloc] initWithFrame:CGRectMake(120, 360, 200, 40)] autorelease];          
        time2.font = [UIFont systemFontOfSize:28];
        time2.textColor = [UIColor whiteColor];
        time2.backgroundColor = [UIColor clearColor];

        time3 = [[[UILabel alloc] initWithFrame:CGRectMake(170, 360, 200, 40)] autorelease];             
        time3.font = [UIFont systemFontOfSize:28];
        time3.textColor = [UIColor whiteColor];
        time3.backgroundColor = [UIColor clearColor];

        time4 = [[[UILabel alloc] initWithFrame:CGRectMake(222, 360, 200, 40)] autorelease];             
        time4.font = [UIFont systemFontOfSize:28];
        time4.textColor = [UIColor whiteColor];
        time4.backgroundColor = [UIColor clearColor];

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit  ;

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"dd-MM-yyyy"];
    NSDate *date = [df dateFromString: eventDateCountDown];

    NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];

    NSInteger year = [dateComponents year];

    NSInteger month = [dateComponents month];

    NSInteger day = [dateComponents day];

    NSLog(@" day day day %d %d %d ",day,month,year);

    NSString *dateStr1 =[[NSString alloc]init];
    dateStr1 = eventDateCountDown;
    // Convert string to date object
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd-mm-yyyy"];
    NSDate *date1 = [dateFormat dateFromString:dateStr1];
    // Convert date object to desired output format
    [dateFormat setDateFormat:@"dd-mm-yyyy"];
    NSString *dateStr = [dateFormat stringFromDate:date1];
    NSLog(@"%@",dateStr1);


    NSString *Currentyear=[[NSString alloc]init];
    Currentyear=[dateStr substringWithRange: NSMakeRange (6,4)];
    int IntCurrentyear=[Currentyear intValue];

    NSString *Currentmth=[[NSString alloc]init];
    Currentmth=[dateStr substringWithRange: NSMakeRange (4,2)];
    int IntCurrentmth=[Currentmth intValue];

    NSString *Currentday=[[NSString alloc]init];
    Currentday=[dateStr substringWithRange: NSMakeRange (0,2)];
    int IntCurrentday=[Currentday intValue];
    NSLog(@"day day day %d %d %d ",IntCurrentday,IntCurrentmth,IntCurrentyear);


    //  NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *date2 = [NSDate date];
    NSTimeInterval todaysDiff = [date2 timeIntervalSinceNow];
    NSTimeInterval futureDiff = [date timeIntervalSinceNow];


    NSString *datetimeEvent =[[NSString alloc]init];
    datetimeEvent =eventDateCountDown;
    NSLog(@"datetimeEvent %@",datetimeEvent);

    NSString *CurrentHour=[[NSString alloc]init];
    CurrentHour=[datetimeEvent substringWithRange: NSMakeRange (0,2)];
    int IntCurrentHour=[CurrentHour intValue];

    NSString *CurrentMinutes=[[NSString alloc]init];
    CurrentMinutes=[datetimeEvent substringWithRange: NSMakeRange (3,2)];
    int IntCurrentMinutes=[CurrentMinutes intValue];
    NSLog(@"CCC day day day %d  %d ",IntCurrentHour,IntCurrentMinutes);

    NSTimeInterval dateDiff = futureDiff - todaysDiff + IntCurrentHour*60*60 + IntCurrentMinutes*60;


    div_t r1 = div(dateDiff, 60*60*24);
    NSInteger theDays = r1.quot;
    NSTimeInterval hourDiff=dateDiff-theDays*60*24*60;
    div_t r2=div(hourDiff,60*60);
    NSInteger theHours = r2.quot;

    NSTimeInterval minDiff=dateDiff-theDays*60*24*60-theHours*60*60;
    div_t r3=div(minDiff,60);
    NSInteger theMins = r3.quot;

    NSTimeInterval secDiff=dateDiff-theDays*60*24*60-theHours*60*60-theMins*60;

    div_t r4=div(secDiff,1);
    NSInteger theSecs = r4.quot;

    if(theDays>9)
    {
        time1.text = [NSString stringWithFormat:@"%2d",theDays];
    }
    else 
    {
        time1.text = [NSString stringWithFormat:@"0%d",theDays];
    }
    if(theHours>9)
    {
        time2.text = [NSString stringWithFormat:@"%2d",theHours];
    }
    else 
    {
        time2.text = [NSString stringWithFormat:@"0%d",theHours];
    }
    if(theMins>9)
    {
        time3.text = [NSString stringWithFormat:@"%2d",theMins];
    }
    else 
    {
        time3.text = [NSString stringWithFormat:@"0%d",theMins];
    }
    if(theSecs >9)
    {
        time4.text = [NSString stringWithFormat:@"%2d",theSecs];
    }
    else 
    {
        time4.text = [NSString stringWithFormat:@"0%d",theSecs];
    }  

    }
    else
    {
        eventDateCountDown = @"";
        NSLog(@"eventCountDown = nil");
    }

    [self.view addSubview:time1];
    [self.view addSubview:time2];
    [scollView addSubview:time3];
    [scollView addSubview:time4];
}

1 个答案:

答案 0 :(得分:0)

您可能每次调用该方法时都会重新创建标签,可能是由于您的eventDateCountDown!= nil check问题。

在“if(eventDateCountDown!= nil){...”块中放置一个断点,并验证它是否被多次调用。