objective c - Most efficient way to have a live countdown -
objective c - Most efficient way to have a live countdown -
i writing app includes countdown. countdown displayed on label, not sure if updating label efficiently. using timer interval of 1 second. there way consume less resources? also, crash on older devices?
- (void)viewdidload { [super viewdidload]; //create calendar nsdates gregorian = [[nscalendar alloc]initwithcalendaridentifier:nsgregoriancalendar]; [gregorian settimezone:[nstimezone timezonewithname:@"est"]]; //create nsdate lunchtime based on = [nsdate date]; nsdatecomponents *compsofnow = [gregorian components:(nsweekdaycalendarunit |nsdaycalendarunit | nshourcalendarunit | nsminutecalendarunit |nsyearcalendarunit|nsmonthcalendarunit|nseracalendarunit)fromdate:now]; [compsofnow sethour:12]; [compsofnow setminute:50]; lunchtime = [gregorian datefromcomponents:compsofnow]; //create timer , set text nstimer *timer = [nstimer scheduledtimerwithtimeinterval:1 target:self selector:@selector(timerticked:) userinfo:nil repeats:yes]; lunchcountdownlabel.text = [nsstring stringwithformat:@"%i:%i:%i", difference.hour, difference.minute, difference.second]; } - (void)timerticked:(nstimer*)timer { = [nsdate date]; difference = [gregorian components:nsyearcalendarunit|nsmonthcalendarunit|nsdaycalendarunit|nshourcalendarunit|nsminutecalendarunit|nssecondcalendarunit fromdate:now todate:lunchtime options:0]; lunchcountdownlabel.text = [nsstring stringwithformat:@"%i:%i:%i", difference.hour, difference.minute, difference.second]; nslog(@"timer ticked");
your input appreciated,
thanks,
hbhargava
this seems fine me... remember invalidate timer 1 time done it.(which not doing of now.)
objective-c ios nstimer
Comments
Post a Comment