我正在使用包装在beginUpdates/endUpdates
中的insertRowsAtIndexPaths/deleteRowsAtIndexPaths
插入/删除表格单元格。我在调整rowHeight时也在使用beginUpdates/endUpdates
。默认情况下,所有这些操作都是动画的。
使用beginUpdates/endUpdates
时如何检测动画已结束
发布于 2012-09-20 23:36:04
那这个呢?
[CATransaction begin];
[CATransaction setCompletionBlock:^{
// animation has finished
}];
[tableView beginUpdates];
// do some work
[tableView endUpdates];
[CATransaction commit];
这是因为tableView动画在内部使用CALayer
动画。也就是说,它们将动画添加到任何打开的CATransaction
中。如果不存在打开的CATransaction
(正常情况下),则隐式开始一个运行循环,并在当前运行循环结束时结束。但是如果你自己开始一个,就像这里做的那样,那么它就会使用那个。
发布于 2019-02-19 01:27:55
如果你的目标是iOS 11或更高版本,你应该改用UITableView.performBatchUpdates(_:completion:)
:
tableView.performBatchUpdates({
// delete some cells
// insert some cells
}, completion: { finished in
// animation complete
})
发布于 2011-12-02 07:12:23
一种可能的解决方案是从调用endUpdates
的UITableView继承并覆盖其setContentSizeMethod
,因为UITableView会调整其内容大小以匹配添加或删除的行。这种方法也应该适用于reloadData
。
为了确保只在调用endUpdates
之后发送通知,还可以覆盖endUpdates
并在其中设置一个标志。
// somewhere in header
@private BOOL endUpdatesWasCalled_;
-------------------
// in implementation file
- (void)endUpdates {
[super endUpdates];
endUpdatesWasCalled_ = YES;
}
- (void)setContentSize:(CGSize)contentSize {
[super setContentSize:contentSize];
if (endUpdatesWasCalled_) {
[self notifyEndUpdatesFinished];
endUpdatesWasCalled_ = NO;
}
}
https://stackoverflow.com/questions/7623771
复制相似问题