UITableViewCell背景色动画

时间:2015-05-09 12:29:01

标签: ios objective-c uitableview uiview uiviewanimation

我想为细胞添加无限变色动画。上面的代码不能正常工作。在发生一点小故障(0.3秒左右)后,它开始使用alpha(不是我先设置的颜色)的颜色制作动画。

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"SuggestionCell"];

    if ([indexPath isEqual:_animatingCellIndexPath]) {
        cell.backgroundColor = [UIColor redColor];

        [UIView animateWithDuration:0.5 delay:0.0
            options:UIViewAnimationOptionAutoreverse 
            | UIViewAnimationOptionRepeat 
            | UIViewAnimationOptionAllowUserInteraction
            animations:^{
                cell.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.4];
            }
            completion:NULL];
    }
    else {
        cell.backgroundColor = [UIColor whiteColor];
    }
}

- (void)tableView:(UITableView *)tableView 
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView deselectRowAtIndexPath:indexPath animated:false];
    _animatingCellIndexPath = indexPath; 
    [tableView reloadRowsAtIndexPaths:@[_animatingCellIndexPath] withRowAnimation:UITableViewRowAnimationNone]; 
}

可以为单元格设置backgroundView并为其设置动画,然后一切正常,但分隔符不是动画。

1 个答案:

答案 0 :(得分:0)

中试用
- (void)tableView:(UITableView *)tableView
  willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath

示例:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    if (indexPath.row == 5) {
        cell.backgroundColor = [UIColor redColor];


    } else {
        cell.backgroundColor = [UIColor blueColor];
    }

    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 5) {
        [UIView animateWithDuration:0.5 delay:0.0
                            options:UIViewAnimationOptionAutoreverse
         | UIViewAnimationOptionRepeat
         | UIViewAnimationOptionAllowUserInteraction
                         animations:^{
                             cell.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.4];
                         }
                         completion:NULL];
    }
}
相关问题