Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

NSTimeInterval + progressView

perlikperlik Posts: 10New Users
Hello All

I am stuck with some issue, I am a new developer and new in Objective-c

So I need display by progress view bar time remain. I am doing billing time circle.


- (void)viewDidLoad
{

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

//1369342800
currentDate = [NSDate date];
destDate = [NSDate dateWithTimeIntervalSince1970:1369342800];
timeRemain = [currentDate timeIntervalSinceDate:destDate];
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];

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

}


According my code above, it should calculate remain time in flot and move progress bar, but it's not

Where I am wrong?

I also put label with remaining time and it's ok.

Thank you

Replies

  • Duncan CDuncan C Posts: 9,116Tutorial Authors, Registered Users @ @ @ @ @ @ @
    edited April 2013
    You need to do a few things differently.
    When you first calculate the total time, you need to save that value. Create a new instance variable totalTime.

    Then you need to set your progress bar to 0, and write code that shifts it towards 1.0 as the time approaches your final time.

    You need to recalculate timeRemain on each pass through your updateProgressDate method prior to updating the progress.

    In your viewDidLoad:
    
    totalTime = [currentDate timeIntervalSinceDate:destDate];
    timeRemain = totalTime;
    CGFloat percentComplete = (totalTime - timeRemain) / totalTime;
    [_progDate setProgress:percentComplete];
    

    And then recalculate percentComplete and set progDate again in your timer method:
    
    timeRemain = [[NSDate date] timeIntervalSinceDate:destDate];
    CGFloat percentComplete = (totalTime - timeRemain) / totalTime;
    [_progDate setProgress:percentComplete];
    
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • perlikperlik Posts: 10New Users
    Hello Duncan Thank You
    Which class totalTime var shoukd be?
  • perlikperlik Posts: 10New Users
    I tried float double but with your example the progress bar always full
  • Duncan CDuncan C Posts: 9,116Tutorial Authors, Registered Users @ @ @ @ @ @ @
    edited April 2013
    perlik said:

    I tried float double but with your example the progress bar always full

    It should not be a class. It should be a scalar data type. Look up the NSDate method timeIntervalSinceDate. Use the data type of that method's return value.

    The code I posted was not tested at all. It was just intended as a guide on what to do. I may have made a mistake in writing it. You are going to need to debug it yourself.

    EDIT: If your end time is more than a few minutes in the future the progress bar will change VERY slowly. If the end time is days in the future you won't see any change in the progress bar for many hours.
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • perlikperlik Posts: 10New Users
    edited May 2013
    Hi Duncan

    Today I have finished my component :)

    So here is the code, maybe will help to someone:
    -(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");
            
        }
        
    }
    
    Post edited by perlik on
  • Duncan CDuncan C Posts: 9,116Tutorial Authors, Registered Users @ @ @ @ @ @ @
    perlik said:

    Hi Duncan

    Today I have finished my component :)

    So here is the code, maybe will help to someone:

    -(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");
            
        }
        
    }
    
    Can you edit your post and put in pre and post tags, as described in the sticky post .How to make your code look nice

    As posted, it's all but impossible to read.

    I fixed the code tags in my quote of your post, above. Look how much easier it is to read!
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • perlikperlik Posts: 10New Users
    done :)
  • perlikperlik Posts: 10New Users
    edited May 2013
Sign In or Register to comment.