2013-04-27 48 views
0

我需要你帮助UIProgressView。xcode set progressView结束日期

我需要更新我的进度视图从当前日期到我选择的结束日期应该是。

此方法只更新标签并显示剩余多少时间来结束时间圆。

-(void)updateProgressDate { 

    NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    int units = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; 
    NSDateComponents *components = [cal components:units fromDate:[NSDate date] toDate:destDate options:0]; 
    [_dateLabel setText:[NSString stringWithFormat:@"%d%c %d%c %d%c %d%c %d%c", [components month], 'm', [components day], 'd', [components hour], 'h', [components minute], 'm', [components second], 's']]; 



} 

-(void)viewDidLoad { 

destDate = [NSDate dateWithTimeIntervalSince1970:1369342800]; 
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateProgressDate) userInfo:nil repeats:YES]; 


} 

我如何实现NSDateComponents到UIProgressView?

感谢

更新时间:

- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 
// Do any additional setup after loading the view. 



    //1369342800 
currentDate = [NSDate date]; 
destDate = [NSDate dateWithTimeIntervalSince1970:1369342800]; 
timeRemain = [destDate timeIntervalSinceDate:currentDate]; 
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateProgressDate) userInfo:nil repeats:YES]; 



} 


-(void)updateProgressDate { 


    NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    int units = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; 
    NSDateComponents *components = [cal components:units fromDate:currentDate toDate:destDate options:0]; 
    //[_dateLabel setText:[NSString stringWithFormat:@"%d%c %d%c %d%c %d%c", [components month], 'm', [components day], 'd', [components hour], 'h', [components minute], 'm']]; 
    [_dateLabel setText:[NSString stringWithFormat:@"%dm %dd %dh %dm", [components month], [components day], [components hour], [components minute]]]; 

    [_progDate setProgress:_progDate.progress = timeRemain]; // here i did += -= right now my progress bar filled but that's not right. my circle 24 day until 24 day next month 

//我了解月份的1%,这是0.03吗?

NSLog([NSString stringWithFormat:@"Remain time: %dm %dd %dh %dm"], timeRemain); 

} 

所以标签是srill会好的,但进度条总是满的。

我在哪里错了?

谢谢

回答

0

您需要使用setProgress:

UIProgessView以0.0到1.0作为参数。你需要100.0计算你的剩余时间,在100因子百分比,然后除以它得到它的范围在0.0到1.0,并将它传递给setProgress:

编辑:

而不是把字符作为参数的它可以用作:

[_dateLabel setText:[NSString stringWithFormat:@"%dm %dd %dh %dm %ds", [components month], [components day], [components hour], [components minute], [components second]]]; 

另外你不应该传递字符串,你需要提供浮动。而NSTimeInterval本身是浮动的。

正如我已经说过的,您需要将时间转换为百分比,然后将其转换为指定的范围。

+0

感谢Anoop,但问题是这里在这一行:[_dateLabel的setText:[的NSString stringWithFormat :[“元件月”,“m”,“元件日”,“d”,“元件小时数”,“h” ',[组件分钟],'米',[组件秒],''']]; _progDate(我的uiprogressview)不能接受字符串。 – Anton 2013-04-27 08:29:17

+0

@Anton:检查更新/编辑 – 2013-04-27 17:28:48

+0

谢谢!我会尝试你的解决方案,并会更新你。 – Anton 2013-04-27 18:21:33

0

所以这是正确的代码,至少我现在可以计算经过的时间,并将其发送给发展观:

-(void)billRangeOperations { 

    today = [NSDate date]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"dd"]; 
    NSString *currentDateFormated = [dateFormatter stringFromDate:today]; 
    int dateFormated = [currentDateFormated intValue]; 
    NSLog(@"Today is: %i", dateFormated); 

    //calculate how much days in current month ****************************************** 


    NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    int units = NSMonthCalendarUnit | NSDayCalendarUnit; 
    NSDateComponents *comps = [[NSDateComponents alloc] init]; 
    comps = [cal components:units fromDate:today]; 
    NSRange days = [cal rangeOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:today]; 
    NSUInteger numberOfDaysInMonth = days.length; 

    NSLog(@"%i Days in current month: %i", numberOfDaysInMonth, comps.month); 

    // end calculating ****************************************************************** 

    // calculating elapsed billing range ************************************************ 

    //dateFormated = 26; //Uncomment this string to check manually, how your billing works if date will be from 25 day of month !!!!!!!!!!!!!!!!!!!!!!!! 


    if (dateFormated < 25) { 

     int remainDaysInCurrentMonth = days.length - 25; 

     NSLog(@"Days Remain to the end of month: %i", remainDaysInCurrentMonth); 

     int elapsedTime = remainDaysInCurrentMonth + dateFormated; 

     NSLog(@"Elapsed Days: %i", elapsedTime); 

     float progress = elapsedTime * 100/days.length; 

     NSLog(@"Progress Days: %f", progress); 

     float progressTimeFormated = progress/100; 

     NSLog(@"Going to ProgressView: %f", progressTimeFormated); 

     // ************************************************************* 

     [progV setProgress:progressTimeFormated animated:YES]; 

     // remain days to the end period to text string ************************************* 

     int remainDaysPeriod = 25 - dateFormated; 

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

     // ********************************************************************************** 

    } else if (dateFormated >= 25) { 

     //***** calculating days in next month ************************* 

     comps.month = comps.month+1; 
     NSRange range = [cal rangeOfUnit:NSDayCalendarUnit 
            inUnit:NSMonthCalendarUnit 
           forDate:[cal dateFromComponents:comps]]; 
     NSLog(@"%i Days in next month: %i", range.length, comps.month); 

     //************************************************************** 


     int remainDaysInCurrentMonth = range.length - 25; 


     NSLog(@"Days Remain in current month: %i", remainDaysInCurrentMonth); 

     int elapsedTime = dateFormated - 25; 

     NSLog(@"Elapsed Time: %i", elapsedTime); 

     float progress = elapsedTime * 100/range.length; 

     NSLog(@"Progress time: %f", progress); 

     float progressTimeFormated = progress/100; 

     NSLog(@"Going to ProgressView: %f", progressTimeFormated); 

     // end calculating elapsed billing range 

     [progV setProgress:progressTimeFormated animated:YES]; 

     // remain days to the end period ******************************************************** 


     int remainDaysPeriod = remainDaysInCurrentMonth+25-elapsedTime; 

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

     // ************************************************************************************** 


     NSLog(@"More than 25"); 

    } 

}