在 UIView 动画中允许交互时遇到问题

发布于 2024-12-12 17:53:55 字数 489 浏览 0 评论 0原文

我有以下代码块来淡出 introView(UIView)

// Hide intro view after 5 seconds
[UIView animateWithDuration: 1.0
          delay: 5.0
        options: (UIViewAnimationOptionAllowUserInteraction |UIViewAnimationOptionCurveLinear)
        animations: ^{
         introView.alpha = 0;
        }
        completion: ^(BOOL finished) {
         [introView removeFromSuperview];
        }];

我在 introVew 中有一个跳过按钮,但没有任何交互,我是否遗漏了一些东西?我必须添加这是一个针对 3.2 的通用应用程序,我正在使用 XCode 4.2

I have the following block of code to fade out an introView(UIView)

// Hide intro view after 5 seconds
[UIView animateWithDuration: 1.0
          delay: 5.0
        options: (UIViewAnimationOptionAllowUserInteraction |UIViewAnimationOptionCurveLinear)
        animations: ^{
         introView.alpha = 0;
        }
        completion: ^(BOOL finished) {
         [introView removeFromSuperview];
        }];

I have a skip button inside the introVew but there is no interaction whatsoever, am I missing something? I have to add this is a Universal app targeting 3.2 and I'm using XCode 4.2

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

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

发布评论

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

评论(5

满身野味 2024-12-19 17:53:55

很确定这在 4.0 之前是不可能的:

UIView userInteractionEnabled 文档

在动画播放期间,用户交互会暂时禁用
动画中涉及的所有视图,无论其值如何
财产。您可以通过指定来禁用此行为
配置时的 UIViewAnimationOptionAllowUserInteraction 选项
动画。

在您尚未发布的应用程序中以 3.2 为目标似乎没有什么意义。

Pretty sure this is impossible pre-4.0:

UIView userInteractionEnabled Docs

During an animation, user interactions are temporarily disabled for
all views involved in the animation, regardless of the value in this
property. You can disable this behavior by specifying the
UIViewAnimationOptionAllowUserInteraction option when configuring the
animation.

There seems little point in targeting 3.2 in an app you haven’t released yet.

—━☆沉默づ 2024-12-19 17:53:55

您是否将按钮 alpha 设置为 0?
如果是的话,这里有一个关于动画的有趣的事情。

您在动画过程中在屏幕上看到的内容并不是应用程序看到的内容。
当您将 alpha 设置为 0 时,该视图的 alpha 为 0,即使您仍然在屏幕上看到它。
此外,alpha 低于 0.05(不记得确切的数字)的视图将不会收到触摸事件。

您可以做的是实现该视图的超级视图的 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 。或 touchesEnded... 如您所愿。
(假设您没有将其 alpha 设置为 0。)

因此您可以测试按钮所在位置发生的触摸,或者只是删除该按钮并让屏幕上的任何触摸取消您的动画。


您可能也对这篇文章感兴趣:
核心动画、意外的动画位置和 hitTest 值

Are you setting your button alpha to 0?
If yes here is an interesting thing about animation.

What you see on the screen during the animation is not what the application sees.
The moment you set your alpha to 0, the alpha is 0 for that view, even if you are still seeing it on the screen.
Also, a view that has an alpha lower that 0.05 (don't recall the exact number) won't get touch event.

What you can do is to implement the - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event of that view's superview. or the touchesEnded... as you like.
(Assuming that your not setting it's alpha to 0.)

So you can test for touche that occur where the button is, or just remove that button and let any touch on the screen cancel your animation.


You may also be interested in this post:
Core Animation, unexpected animated position and hitTest values

葬花如无物 2024-12-19 17:53:55

我发现了另一种可能导致这种情况的情况。我在其他地方没有看到这个答案。它根本不涉及阿尔法。

如果您在调用 UIView.animate() 时使用延迟,那么即使您指定 .allowUserInteraction 选项,视图在延迟期间也不会接收触摸。我不知道为什么,但我可以通过将代码块移动到另一个函数,并在相同的延迟秒后使用执行选择器来帮助它,并且在块中我毫不延迟地运行代码。

I found another circumstance which could cause this. I haven't seen this answer anywhere else. It does not deal with alpha at all.

If you use a delay in the call to UIView.animate(), then even if you specify the .allowUserInteraction option, the view does NOT receive touches during the delay period. I have no idea why, but I could help it by moving the code block to another function, and using a performSelector after the same delay seconds, and in the block I run the code without delay.

〆一缕阳光ご 2024-12-19 17:53:55

我对通过更改 Alpha 进行动画处理的按钮也遇到了同样的问题。引用文斯伯恩的回答......

您在动画过程中在屏幕上看到的内容并不是应用程序看到的内容。当您将 alpha 设置为 0 时,该视图的 alpha 也为 0,即使您仍然在屏幕上看到它。
并且 alpha 低于 0.05(不记得确切的数字)的视图将不会获得触摸 > 事件。

…将最小 alpha 设置为 0.1 而不是 0.0 的简单解决方案对我来说很有效:

[UIView animateWithDuration:0.5
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         self.myButton.alpha = 0.1f;
                     }
                     completion:^(BOOL finished){
                     }]

Button 始终注册 touchUpInside,无需其他方法,并且将 alpha 设为 0 在外观上几乎没有任何区别。

I had the same problem with a button that I animated with changing the alpha. Cueing off VinceBurn's answer...

What you see on the screen during the animation is not what the application sees. The moment >you set your alpha to 0, the alpha is 0 for that view, even if you are still seeing it on the >screen.
AND view that have an alpha lower that 0.05 (don't recall the exact number) won't get touch >event.

… the simple solution of just making the minimum alpha 0.1 instead of 0.0 worked for me:

[UIView animateWithDuration:0.5
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         self.myButton.alpha = 0.1f;
                     }
                     completion:^(BOOL finished){
                     }]

Button registered the touchUpInside all the time with no additional method needed, and there was virtually no difference in appearance from taking the alpha to zero.

避讳 2024-12-19 17:53:55

这在 iOS 3.2 中不起作用,因为块仅在 iOS4 中可用
您必须在单独的线程中使用标准动画技术,这样就不会阻塞界面

[UIView beginAnimations:nil context:nil];

[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:view cache:YES];

[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

[UIView setAnimationDuration:1.0];

[view1 setHidden:TRUE];

[UIView commitAnimations];

This won't work in iOS 3.2 since Blocks are only available in iOS4
you will have to use the standard animation techniques, in a separate thread so that you don't block the interface

[UIView beginAnimations:nil context:nil];

[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:view cache:YES];

[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

[UIView setAnimationDuration:1.0];

[view1 setHidden:TRUE];

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