xcode按结束日期设置progressView

时间:2013-04-27 08:01:08

标签: iphone ios nsdate uiprogressview

我需要你帮助UIProgressView。

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

此方法仅更新标签并显示剩余时间到结束时间圈。

-(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);

}

所以标签是正常的,但进度条总是满的。

我哪里错了?

由于

2 个答案:

答案 0 :(得分:0)

您需要使用setProgress:

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

编辑:

不是将char作为参数,而是可以用作:

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

你也不应该传递字符串,你需要提供浮动。并且NSTimeInterval本身处于浮动状态。

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

答案 1 :(得分: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");

    }

}