2015-05-09 107 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