表格视图单元格加载animation一个接一个

我需要animation表视图行的负载。 当表重新加载数据时,我需要从左到右进入的行。 我怎样才能做到这一点?

在你的tableview委托中,

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

把这个从下到上的淡入翻译animation(从Anbu.Karthik答案简化),

  //1. Define the initial state (Before the animation) cell.transform = CGAffineTransformMakeTranslation(0.f, HOME_ADS_CELL_HEIGHT); cell.layer.shadowColor = [[UIColor blackColor]CGColor]; cell.layer.shadowOffset = CGSizeMake(10, 10); cell.alpha = 0; //2. Define the final state (After the animation) and commit the animation [UIView beginAnimations:@"rotation" context:NULL]; [UIView setAnimationDuration:0.5]; cell.transform = CGAffineTransformMakeTranslation(0.f, 0); cell.alpha = 1; cell.layer.shadowOffset = CGSizeMake(0, 0); [UIView commitAnimations]; 

为了更好的用户体验,build议animation应该只播放一次 ,直到表视图被解除分配。

把上面的代码放进去

 if (![self.shownIndexes containsObject:indexPath]) { [self.shownIndexes addObject:indexPath]; // Your animation code here. } 

——- Swift 3版本—————————————- ————————————————– ———

 var shownIndexes : [IndexPath] = [] func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { if (shownIndexes.contains(indexPath) == false) { shownIndexes.append(indexPath) cell.transform = CGAffineTransform(translationX: 0, y: HOME_ADS_CELL_HEIGHT) cell.layer.shadowColor = UIColor.black.cgColor cell.layer.shadowOffset = CGSize(width: 10, height: 10) cell.alpha = 0 UIView.beginAnimations("rotation", context: nil) UIView.setAnimationDuration(0.5) cell.transform = CGAffineTransform(translationX: 0, y: 0) cell.alpha = 1 cell.layer.shadowOffset = CGSize(width: 0, height: 0) UIView.commitAnimations() } } 

tableView:willDisplayCell:forRowAtIndexPath方法将在每次显示单元格时被调用,并且由于它们同时被查看,这意味着它们在不同的线程中被调用,并且不能让iOS SDK调用此方法方法顺序。 所以我想要得到你想要的东西的方法是在每个细胞显示时设置一个延迟。

 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ CGFloat delay = indexPath.row * yourSupposedAnimationDuration; [UIView animateWithDuration:yourSupposedAnimationDuration delay:delay options:UIViewAnimationOptionCurveEaseIn animations:^{ //Your animation code }completion:^(BOOL finished) { //Your completion Code }]; } 

这里是我的Swift 3解决scheme,用于逐个显示单元格。 有什么好处的是,它们只在第一次加载时加载,并且只在最初显示的单元格(当用户向下滚动时不会运行)。

请享用 :)

 private var finishedLoadingInitialTableCells = false func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { var lastInitialDisplayableCell = false //change flag as soon as last displayable cell is being loaded (which will mean table has initially loaded) if yourTableData.count > 0 && !finishedLoadingInitialTableCells { if let indexPathsForVisibleRows = tableView.indexPathsForVisibleRows, let lastIndexPath = indexPathsForVisibleRows.last, lastIndexPath.row == indexPath.row { lastInitialDisplayableCell = true } } if !finishedLoadingInitialTableCells { if lastInitialDisplayableCell { finishedLoadingInitialTableCells = true } //animates the cell as it is being displayed for the first time cell.transform = CGAffineTransform(translationX: 0, y: self.rowHeight/2) cell.alpha = 0 UIView.animate(withDuration: 0.5, delay: 0.05*Double(indexPath.row), options: [.curveEaseInOut], animations: { cell.transform = CGAffineTransform(translationX: 0, y: 0) cell.alpha = 1 }, completion: nil) } }