美文网首页
解决UITableViewCell展开动画位移错位

解决UITableViewCell展开动画位移错位

作者: 凉秋落尘 | 来源:发表于2019-12-03 14:24 被阅读0次

iOS在项目中,常会用到的一个需求就是,UITableView列表展开看全文,增加展开和收缩的动画。

第一种动画方式

系统提供了动画效果,比如我们刷新一行:

[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

系统为我们提供UITableViewRowAnimation各种过渡动画效果:

typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {
    UITableViewRowAnimationFade,
    UITableViewRowAnimationRight,           // slide in from right (or out to right)
    UITableViewRowAnimationLeft,
    UITableViewRowAnimationTop,
    UITableViewRowAnimationBottom,
    UITableViewRowAnimationNone,            // available in iOS 3.0
    UITableViewRowAnimationMiddle,          // available in iOS 3.2.  attempts to keep cell centered in the space it will/did occupy
    UITableViewRowAnimationAutomatic = 100  // available in iOS 5.0.  chooses an appropriate animation style for you
};

可以根据自己需要的效果去更换类型,然后改边cell行高就可以实现。

但是使用这种方式有的会出现cell的错位问题,这是苹果的动画bug。这个问题是因为动画过程中,预估高度带来的原因。
解决这个错位的问题,可以提前保存预先存预估高度来解决:

@property (nonatomic, strong) NSMutableDictionary *cellHeightsDictionary;

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.cellHeightsDictionary setObject:@(cell.frame.size.height) forKey:indexPath];
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSNumber *height = [self.cellHeightsDictionary objectForKey:indexPath];
    if (height) return height.doubleValue;
    return UITableViewAutomaticDimension;
}

这样可以解决错位的问题,但是有的会出现另外一个问题,就是使用cell复用时,点击屏幕最上面的cell时,也会出现错位问题。显然使用UITableViewRowAnimation还是各种问题出来,因此建议使用静态更新的动画来解决:

第二种动画方式
[self.tableView beginUpdates];
[self.tableView endUpdates];

只需要两行,就可以实现更改高度平滑的动画效果。但是需要注意一点,动画的执行过程是更改cell的高度的动画,内部的高度在没有做好约束的前提下,可能效果不会很好。如果使用layout的方式进行约束或者使用autoresizingMask,让内部的控件比如说cell上面的一个label去适配cell的高度变化。就会自然而然的变。
但是如果没有做约束的话,比如直接用纯代码去定义label.frame,最后的显示效果也会有问题。
第二种方式,在中间去对需要更改的frame做动画效果达到想要的方法:

UITableView *cell = [tableView cellForRowAtIndexPath:indexPath];
[self.tableView beginUpdates];

[UIView animateWithDuration:0.25 animations:^{
     cell.textLabel.frame = CGRectMake(0, 0,FOScreenW, 50);
}];

[self.tableView endUpdates];

建议使用第一种方式,直接做好layout适配,就可以了。到这里使用系统的方式进行展开收缩动画,就不会有问题,效果非常好。

题外:如果想在endUpdates动画结束后,做其他的操作呢,比如去隐藏或显示控件之类的操作。可以使用CATransaction,具体的代码方式:

[CATransaction begin];
[CATransaction setCompletionBlock:^{
// 在这里做动画结束后的操作
    [cell isOpenOrClose];
}];
[self.tableView beginUpdates];
[self.tableView endUpdates];
[CATransaction commit];
第三种动画方式

如果前面两种你都不想要,那就只能去自定义的方式,最简单粗暴的,用动画只改变当前cell的frame值:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

UITableView *cell = [tableView cellForRowAtIndexPath:indexPath];

[UIView animateWithDuration:0.25 animations:^{
     cell.frame = CGRectMake(0, 0,FOScreenW, 100);
      [cell setNeedsLayout];
       [cell layoutIfNeeded];
// 并且要更新底下的cell,否则会被覆盖掉
      NSArray *cells = [tableView visibleCells];
      for (UITableViewCell *vis_cell in cells) {
        if (![vis_cell isEqual:cell]) {
             vis_cell.frame = CGRectMake(0, 0,FOScreenW, 50); 
             [vis_cell setNeedsLayout];
             [vis_cell layoutIfNeeded];
        }
    }
} completion:^(BOOL finished) {
       [tableView reloadData];
    }];
}

相关文章

网友评论

      本文标题:解决UITableViewCell展开动画位移错位

      本文链接:https://www.haomeiwen.com/subject/bihcgctx.html