[UIView animateWithDuration:delay: 如何在退出时删除

发布于 2024-12-17 21:06:54 字数 903 浏览 3 评论 0原文

当用户点击主页按钮时,我的应用程序挂起: 导致问题的代码片段是这样的:

[UIView animateWithDuration:0.5
                      delay: (float)random()/RAND_MAX * 1.0f
                    options: UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
                 animations:^{
                     [self setAlpha:1.0f];
                 }
                 completion:^(BOOL finished){
                 }];

我相信发生的事情是随机延迟安排动画,然后用户退出 - 所以删除动画的方法(它设置为自动重复)永远不会被调用?并且应用程序挂起,同时仍显示动画。

情况太糟糕了,需要重新启动 iPhone。我之前在块和动画方面也遇到过类似的问题,所以这是我评论的第一个代码,问题就消失了。

我尝试在点击主页按钮后在视图上调用 [self.layer remove allAnimations] ,但这没有帮助。

我尚未成功调试该问题,因为它发生在调用 applicationWillEnterbackground 后的“无人区”中。仪器显示没有任何异常。

有人可以提供有关我应该采取什么方法来确保延迟动画不会导致此行为的帮助吗?

(可能是幕后的performSelector: withDelay):

I am experiencing my application hangs when the user taps the home button:
The snippet causing the problem is this:

[UIView animateWithDuration:0.5
                      delay: (float)random()/RAND_MAX * 1.0f
                    options: UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
                 animations:^{
                     [self setAlpha:1.0f];
                 }
                 completion:^(BOOL finished){
                 }];

I believe what is happening is that the random delay schedules the animation, the user then quits - so the method that removes the animation (it is set to autorepeat) is never called? and the app hangs, while still showing the animation.

It is so bad that a restart of the iPhone is needed. I had similar issues with blocks and animations before so this was the first code I out-commented and the problem went away.

I have tried calling [self.layer remove allAnimations] on the view upon tapping the home button, but it does not help.

I have not been successful debugging the issue as it happens in "no-mans-land" after applicationWillEnterbackground has been called. Instruments shows nothing out of the ordinary.

Can someone offer help regarding what approach I should take to make sure animations with delays does not cause this behavior?

(it might be performSelector: withDelay behind the scenes):

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

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

发布评论

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

评论(2

我不是你的备胎 2024-12-24 21:06:54

向视图层发送removeAllAnimations 调用应删除动画。

您说“我尝试在点击主页按钮后在视图上调用 [self.layer remove allAnimations],但这没有帮助。”

您不会在视图上调用 self,您会自己将该消息发送到视图的层。

从视图控制器来看,代码可能看起来像这样:

[myAnimatedView.layer removeAllAnimations];

顺便说一句,您发布的代码驻留在哪里?它需要从您正在制作动画的视图的实例方法运行才能工作。

Sending a removeAllAnimations call to the view's layer should remove the animation.

You said "I have tried calling [self.layer remove allAnimations] on the view upon tapping the home button, but it does not help."

You would not call self on the view, you would send that message to the view's layer yourself.

From the view controller, the code might look something like this:

[myAnimatedView.layer removeAllAnimations];

BTW, where does the code that you posted reside? It would need be run from an instance method of the view that you are animating in order to work.

池予 2024-12-24 21:06:54

如果你想从“自我”到达基础层,你会这样做:

[self.view.layer removeAllAnimations];

If you wanted to reach the base layer from "self" you'd do:

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