首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何检测动画在UITableView开始更新/结束更新时结束?

如何检测动画在UITableView开始更新/结束更新时结束?
EN

Stack Overflow用户
提问于 2011-10-02 08:06:57
回答 4查看 33.7K关注 0票数 119

我正在使用包装在beginUpdates/endUpdates中的insertRowsAtIndexPaths/deleteRowsAtIndexPaths插入/删除表格单元格。我在调整rowHeight时也在使用beginUpdates/endUpdates。默认情况下,所有这些操作都是动画的。

使用beginUpdates/endUpdates时如何检测动画已结束

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 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 (正常情况下),则隐式开始一个运行循环,并在当前运行循环结束时结束。但是如果你自己开始一个,就像这里做的那样,那么它就会使用那个。

票数 296
EN

Stack Overflow用户

发布于 2019-02-19 01:27:55

如果你的目标是iOS 11或更高版本,你应该改用UITableView.performBatchUpdates(_:completion:)

tableView.performBatchUpdates({
    // delete some cells
    // insert some cells
}, completion: { finished in
    // animation complete
})
票数 7
EN

Stack Overflow用户

发布于 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;
    }
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7623771

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档