iOS: Counting upwards animation
I want to animate my UILabel such that it looks like its counting upward.
For arguments sake lets just say I want it to go up by 1 every second.
None of the ways below worked properly.
A simple for loop (example here) doesn't work because its way too fast.
for(int i =0;i<1000;i++)
{
lblNum.text = [NSString stringWithFormat:@"%d",i];
}
Adding in sleep(1) doesn't work because the executions are asynchronous (I
think thats why at least)
I also tried:
for(int i=0;i<1000;i++)
{
[self performSelector:@selector(updateLbl:)
withObject:[NSNumber numberWithInt:i ] afterDelay:1];
}
-(void)updateLbl:(NSNumber *)num
{
lblNum.text = [NSString stringWithFormat:@"%@",num];
}
As well as:
for(int i=0;i<1000;i++)
{
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW,
0), ^{
// Do something...
sleep(1);
dispatch_async(dispatch_get_main_queue(), ^{
lblNum.text = [NSString stringWithFormat:@"%d",i];
});
});
}
No comments:
Post a Comment