2015-05-09 90 views
2

我想为单元格添加无限变色动画。上述代码无法正常工作。它在一点毛刺(0.3秒左右)后开始用alpha(不是我先设置的颜色)进行动画处理。UITableViewCell背景色动画

- (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细胞和动画,然后一切工作正常,除了隔膜没有动画。

回答

0

在 试试吧 - (空)的tableView:(UITableView的*)的tableView willDisplayCell:(*的UITableViewCell)单元 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]; 
    } 
} 
+0

我已经试过了。没有帮助。 – user1561346

+0

我编辑了一个为我工作的例子。如果它仍然不适合你,请上传你的代码,并描述会发生什么(可能问题根本与此无关) –

+0

谢谢。是的,它工作,如果表刚刚加载。尝试将'indexPath.row == 5'更改为'[indexPath isEqual:_animatingCellIndexPath]'并添加此代码wor animation start:' - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [ tableView deselectRowAtIndexPath:indexPath animated:false]; _animatingCellIndexPath = indexPath; [tableView reloadRowsAtIndexPaths:@ [_ animatingCellIndexPath] }' – user1561346