如何禁用 UIView 动画块中嵌套的代码的动画?

发布于 2024-12-06 13:51:44 字数 152 浏览 0 评论 0原文

有一个方法由第三方 API 在动画块内调用。在这种方法中,我应该构建一些子视图。但在这种情况下,我不希望在构造子视图时发生动画。

有没有办法说“[UIView dontAnimateFromHere] ... [UIView nowYouMayAnimateAgain]”?

There's a method which is called inside an animation block by a third party API. In that method I'm supposed to build some subviews. But in this case I don't want animation to happen when constructing the subviews.

Is there a way of saying "[UIView dontAnimateFromHere] ... [UIView nowYouMayAnimateAgain]"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

内心荒芜 2024-12-13 13:51:44

是的,确实有这样的方法。就像这样:

[UIView setAnimationsEnabled:NO];
// Animations happen here
[UIView setAnimationsEnabled:YES];

...这将禁用通过块触发的 UIView 动画和使用旧的开始/结束方法触发的动画。

也就是说,我假设您的第三方库是预编译的,否则您可以直接修改源代码:当然有可能它正在做一些奇怪的事情并以另一种方式进行动画处理,因此您的里程可能会因该解决方案而异。

这不会禁用动画块中所做的更改:它们会立即发生。否则,您将面临发生不好的事情的风险,因为您的第三方 API 会对哪些视图可能不正确做出假设。

Yes indeed, there is such a way. It's like this:

[UIView setAnimationsEnabled:NO];
// Animations happen here
[UIView setAnimationsEnabled:YES];

...this will disable both UIView animations triggered via blocks and animations triggered using the old begin/end methods.

That said, I'm assuming your third party library is pre-compiled otherwise you could modify the source directly: it is of course possible it's doing something weird and animating in another way, so your mileage may vary with this solution.

This won't disable the changes being made in the animation blocks: they'll simple happen immediately. Otherwise you'd risk bad things happening since your third party API would be making assumptions about where views might be that weren't true.

染柒℉ 2024-12-13 13:51:44

对于 iOS 7 及更高版本,有此 UIView 的 +performWithoutAnimation:

请注意,performWithoutAnimation 对于在动画块内立即执行更改非常有用,但它不会禁用嵌套块内进行的动画调用,因此使用它是为了方便,但它不那么强大作为原始答案的 setAnimationsEnabled

For iOS 7 and later, there is this UIView's +performWithoutAnimation:.

Note that performWithoutAnimation is useful for immediately executing a change while you are within an animation block, but it will not disable animation calls made within the nested block, so use it for convenience, but it is not as robust as setAnimationsEnabled of the original answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文