0

我需要你帮助 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);

}

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

我哪里错了?

谢谢

4

2 回答 2

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 本身是浮动的。

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

于 2013-04-27T08:05:37.293 回答
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");

    }

}
于 2013-05-08T18:07:18.560 回答