Edgar
Edgar

Reputation: 927

Custom UITableView Cell Animation

I am trying to make custom cell highlighted animation...but it does not work. Any thoughts how can I animate .alpa change?

ViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"myID";
    TableViewCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    cell.selectedImg.frame = cell.contentView.frame;
    cell.selectedImg.image = [ViewController imageFromColor:[UIColor colorWithRed:0.55 green:0.95 blue:0.68 alpha:0.0]];
    cell.selectedImg.hidden = NO;

    return cell;
}

TableViewCustomCell.m

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    if (highlighted) {
        [UIView animateWithDuration:0.2 animations:^(void) {
            self.selectedImg.alpha = 0.5;}];
    }
    else {
         [UIView animateWithDuration:0.2 animations:^(void) {
            self.selectedImg.alpha = 0.0;}];
    }
}

P.S. I will have a lot of UITableViews and they all will have the same cell style, so Should I place cell.selectedImg code somewhere in TableViewCustomCell.m? If yes, where is the right place?

Upvotes: 0

Views: 215

Answers (1)

Amit Singh
Amit Singh

Reputation: 428

Try to replace the code in

  • (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated

in TableViewCell.m with the following code:

[UIView animateWithDuration:0.2
                          delay:0
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         if (highlighted) {
                           self.selectedImg.alpha = 0.5;
                         }
                         else {
                           self.selectedImg.alpha = 0;
                         }
                     }
                     completion:^(BOOL finished){
                         if (highlighted) {
                          self.selectedImg.alpha = 0;
                         }
                         else {
                          self.selectedImg.alpha = 0.5;
                         }
}];

Upvotes: 1

Related Questions