Reputation: 59
I have a table view that is broken up into alphabetic sections. I am displaying an animated banner in a UIImage View in the footer of each section, and need to determine which image is displayed when the UIImage View is clicked. I set a timer right before I make the call to startAnimating. The timer is to fire every 5 seconds, at the same rate that the animation changes, but the timer is firing much faster. Sometimes it will fire 2 or 3 times within that 5 second period. Here is the code where I start the timer and the animation:
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger) section {
...
imgAdBar = [[bannerView alloc]initWithFrame:CGRectMake(footer.frame.origin.x,footer.frame.origin.y,footer.frame.size.width,footer.frame.size.height)];
imgAdBar.image=[UIImage imageNamed:[NSString stringWithFormat:@"%@", [animationArray objectAtIndex:0]]];
[imgAdBar saveBannerArray:animationArray];
[imgAdBar setUserInteractionEnabled:YES];
imgAdBar.animationImages = images;
imgAdBar.animationDuration=[images count]*5;
imgAdBar.animationRepeatCount=0;
timerCount=0;
[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(timerRunning:) userInfo:nil repeats:YES];
[imgAdBar startAnimating];
[footer addSubview:imgAdBar];
footer.backgroundColor = [UIColor clearColor];
}
return footer;
}
And here is the selector:
-(void)timerRunning:(NSTimer*)theTimer
{
NSLog(@"timerCount=%d",timerCount);
imgAdBar.timerCount=timerCount;
if (timerCount==numAnimationImages) {
timerCount=0;
}
NSLog(@"currentImage=%@",[animationArray objectAtIndex:timerCount]);
timerCount++;
}
I plan to use that timer to index into my image array so that I can tell which is displayed. Anyone have any idea why it's not firing when it should? Thanks for any assistance!
Upvotes: 2
Views: 1599
Reputation: 726569
Since you do not use the value of timerCount
unless there is a click, you do not need to update it on timer: it is sufficient to store the time at the moment when you start animation. Knowing that each image is shown for five seconds, you can calculate the index of the image being displayed by taking the difference, in seconds, between the time of the click and the time when you started the animation, dividing it by five, and taking the remainder of the division by the total number of images.
Say you have ten images, each displaying for five seconds in a loop. Let's also say that the animation has started at 08:15:51
. Now let's say there's a click at 08:19:23
, or 212
seconds after the animation has started. After dividing by five you get 42
; taking the remainder of the division by ten, you get 2
. Therefore, you know that the user has clicked on the third image in the animation loop (as usual, indexes start from zero).
Upvotes: 1
Reputation: 7074
You have to use NSTimer as an property ...
Since viewForFooterInSection
is called several times for many sections you have to invalidate before you reinitialize it or you have to check for null following is the code..
Invalidate:
NSTimer *timer; // declare in ViewDidLoad
// code in viewForFooterInSection
[timer invalidate];
timer = [NSTimer scheduledTimerWithTimeInterval: 0.5
target: self
selector: @selector(handleTimer:)
userInfo: nil
repeats: YES];
Check for nil
if (timer == nil) {
timer = [NSTimer scheduledTimerWithTimeInterval: 0.5
target: self
selector: @selector(handleTimer:)
userInfo: nil
repeats: YES];
}
Hope it should help..
Upvotes: 1
Reputation: 1480
Declare an NSTimer as property in your header file
@propterty (nonatomic, retain) NSTimer *someTimer;
In the line where you fire the timer
someTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(timerRunning:) userInfo:nil repeats:YES];
Don't forget to release it in -(void)viewDidUnload
[someTimer release];
Upvotes: 1