Reputation: 29
I am trying to animate tableview
cell's imageview
which is present by default, i want the image view to be animated only once when the cell is visible, for this i have written the following code.
- (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];
}
NSArray *myImageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:[NSString stringWithFormat:@"wave6.png"]],
[UIImage imageNamed:[NSString stringWithFormat:@"wave7.png"]],
[UIImage imageNamed:[NSString stringWithFormat:@"wave8.png"]],
[UIImage imageNamed:[NSString stringWithFormat:@"wave9.png"]],
[UIImage imageNamed:[NSString stringWithFormat:@"wave10.png"]],nil];
NSArray *titles = @[@"John Appleseed", @"John Doe", @"Test User",@"John Appleseed", @"John Doe", @"Test User",@"John Appleseed", @"John Doe", @"Test User",
@"Test User",@"John Appleseed", @"John Doe", @"Test User",@"John Appleseed", @"John Doe", @"Test User",@"John Appleseed", @"John Doe", @"Test User",
@"Test User"];
[cell.imageView setAnimationImages:myImageArray];
cell.imageView.animationDuration = 4.20;
cell.imageView.animationRepeatCount = 1;
[cell.imageView startAnimating];
cell.textLabel.text = titles[indexPath.row];
return cell;
}
Upvotes: 0
Views: 1031
Reputation: 11
You can use animate method in custom cell class
like this
@interface CustomCell ()
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
isAnimating = NO;
[self startAni];
}
return self;
}
- (void)startAni{
if (!isAnimating){
isAnimating = YES;
}
//Animation Code here
}
- (void)stopAni{
if (isAnimating){
isAnimating = NO;
}
//Animation Code here
}
Upvotes: 0
Reputation: 1224
Create object of UIImageView and add on cell contentView
UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageFrame];
imageView.animationImages = myImageArray;
imageView.animationDuration = 4.20;
imageView.animationRepeatCount = 1;
imageView.image = [UIImage imageNamed:@"wave5.png"]
; // you can put any image
[imageView startAnimating];
[cell.contentView addSubview:imageView];
Upvotes: 1
Reputation: 25692
1. Remove the animation code in cellforatindexpath
2. animate it when cell is displaying
3. stop animation while hiding the cells
4. Will Improve the performance of the App
5. Save more memory
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *myImageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:[NSString stringWithFormat:@"wave6.png"]],
[UIImage imageNamed:[NSString stringWithFormat:@"wave7.png"]],
[UIImage imageNamed:[NSString stringWithFormat:@"wave8.png"]],
[UIImage imageNamed:[NSString stringWithFormat:@"wave9.png"]],
[UIImage imageNamed:[NSString stringWithFormat:@"wave10.png"]],nil];
[cell.imageView setAnimationImages:myImageArray];
cell.imageView.animationDuration = 4.20;
cell.imageView.animationRepeatCount = 1;
[cell.imageView startAnimating];
}
-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
[cell.imageView stopAnimating];
[cell.layer removeAllAnimations];
}
instead of
[NSString stringWithFormat:@"wave6.png"]
you could specify just like @"wave6.png"
Upvotes: 4