I am trying to add animation for UIImages which are added in an array. I would like to add animation to array index 0 and make it animated.I want to change the imageCount and animate it every 3 secs. I have also added NSTimer which will help me to change the image but it is not changing the image correctly. I have like a menu list in which the first link should be animated. Has anyone come across such an issue before and can help me out.
- (void)viewDidLoad {
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:3
target:self
selector:@selector(animateFunction)
userInfo:nil
repeats:YES];
[self setListArray:[NSMutableArray arrayWithObjects:@"",@"List",@"To-Do",nil]];
}
-(void)animateFunction
{
NSLog(@"Timer heartbeat %i",imageCount);
if (imageCount == 2) {
// set the image count back to initial value;
imageCount = 1;
} else {
imageCount++;
}
[self setCellIconNames:[NSArray arrayWithObjects:[NSString stringWithFormat:@"image%i.png", imageCount],@"final.png",@"blue.png",nil]];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
NSString *cellIconName = [[self cellIconNames] objectAtIndex:[indexPath row]];
UIImage *cellIcon = [UIImage imageNamed:cellIconName];
[[cell imageView] setImage:cellIcon];
cell.textLabel.text = [listArray objectAtIndex:row];
cell.backgroundColor = [UIColor clearColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
I believe the problem is that
imageCountis being set to 1 each time you call the method associated with the timer. Try declaringimageCountin the header file as something likeint imageCount;, removeimageCount = 1;from theanimateFunctionmethod and place it in theviewDidLoador some similar method to initialize it. Then when you iterateimageCount(which you can substituteimageCount = imageCount + 1with justimageCount++if you like), it will add 1 to the variable properly instead of returning 2 each time (which is what the current method does). If you need the images to loop, you will need to set up a check that resets theimageCountlike the following:Hopefully that does the trick for you