【问题标题】:UITableViewCell background color animationUITableViewCell 背景色动画
【发布时间】:2015-07-20 07:39:50
【问题描述】:

我想为单元格添加无限变色动画。上面的代码不能正常工作。在出现小故障(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 并为其设置动画,然后一切正常,除了分隔符没有动画。

【问题讨论】:

    标签: ios objective-c uitableview uiview uiviewanimation


    【解决方案1】:

    试一试

    - (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];
        }
    }
    

    【讨论】:

    • 我编辑了一个对我有用的示例。如果它仍然不适合您,请上传您的代码,并描述发生了什么(也许问题与此无关)
    • 谢谢。是的,如果表刚刚加载,它就可以工作。尝试将 indexPath.row == 5 更改为 [indexPath isEqual:_animatingCellIndexPath] 并在动画开始时添加此代码:- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:false]; _animatingCellIndexPath = indexPath; [tableView reloadRowsAtIndexPaths:@[_animatingCellIndexPath] }
    • 评论被截断了一点。查看更新的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    • 2010-09-30
    相关资源
    最近更新 更多