动画期间无法触摸(块动画)

发布于 2024-12-10 03:40:41 字数 439 浏览 4 评论 0原文

我有一个带有 UIButtonUITextField 的视图和一个用于背景的 UIImageView

在 viewDidLoad 中,我尝试使用块将 UIImageView 从 alpha=0 动画到 alpha =1。这是非常基本的,这是代码:

[UIView animateWithDuration:1.5 animations:^(void){

    ((UIView*)[self.view viewWithTag:123]).alpha = 1;
}completion:^(BOOL finished){
}];

运行良好。但在那 1.5 秒的动画中,我在当前视图中的触摸似乎被禁用了。在动画完成之前我无法单击任何按钮或文本字段。

提前致谢

I have a View with UIButton, UITextField, and a UIImageView for Background.

in viewDidLoad i try animate UIImageView from alpha=0 to alpha =1 using block. it's pretty basic, here's the code:

[UIView animateWithDuration:1.5 animations:^(void){

    ((UIView*)[self.view viewWithTag:123]).alpha = 1;
}completion:^(BOOL finished){
}];

which is working fine. but during that 1.5 seconds of animation, my touch in current view seems to be disabled. i can't click any of the buttons or textFields until animation finish.

Thanks in Advance

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

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

发布评论

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

评论(6

攀登最高峰 2024-12-17 03:40:41

您应该使用选项UIViewAnimationOptionAllowUserInteraction,如下例所示:

[UIView animateWithDuration:1.0 
                      delay:0 
                    options:UIViewAnimationOptionAllowUserInteraction 
                 animations:^{ myView.alpha = 0.5; } 
                 completion:NULL];

You should use option UIViewAnimationOptionAllowUserInteraction as in the following example:

[UIView animateWithDuration:1.0 
                      delay:0 
                    options:UIViewAnimationOptionAllowUserInteraction 
                 animations:^{ myView.alpha = 0.5; } 
                 completion:NULL];
你怎么这么可爱啊 2024-12-17 03:40:41

对于 Swift 3+,iOS 10 使用 .allowUserInteraction 动画中的 UIView 用户交互

添加到动画调用 UIViewAnimationOptions 标志 .allowUserInteraction

UIView.animate(withDuration: 1.0, delay: 0.0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.0, options: [.curveEaseIn,.allowUserInteraction], animations: {

            //your animation block

            }, completion: { isAnimated in
            //after animation is done
    })

UIView userinteraction in animation using .allowUserInteraction for Swift 3+,iOS 10

Add to the animation call the UIViewAnimationOptions flag .allowUserInteraction

UIView.animate(withDuration: 1.0, delay: 0.0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.0, options: [.curveEaseIn,.allowUserInteraction], animations: {

            //your animation block

            }, completion: { isAnimated in
            //after animation is done
    })
你的笑 2024-12-17 03:40:41

看起来动画按钮通常会使它们的点击状态变得非常挑剔。我为此提出的解决方案是让动画元素只是一个具有所有按钮样式的 UIView。在 UIView 动画之前,我向 UIView 上方的视图添加一个真实的按钮,并在我希望用户交互发生的位置提供清晰的背景。确实增加了一个额外的步骤,但非常可靠。

//create button and view
UIButton *viewNotificationBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, ([UIScreen mainScreen].bounds.size.height -70), 320, 50)];
UIView *viewNotificationView = [[UIView alloc] initWithFrame:CGRectMake(0, ([UIScreen mainScreen].bounds.size.height), 320, 50)];

//add elements to screen
[self.view addSubview:viewNotificationView];
[self.view insertSubview:viewNotificationBtn aboveSubview:viewNotificationView];


[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
    [viewNotificationView setFrame:CGRectMake(0, ([UIScreen mainScreen].bounds.size.height - 70), 320, 50)];
} completion:^(BOOL finished) {
    [UIView animateWithDuration:0.5 delay:10.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
        viewNotificationView.alpha = 0;
    } completion:^(BOOL finished) {
        [viewNotificationView removeFromSuperview];
        [viewNotificationBtn removeFromSuperview];
    }];
}];

Seems like animating buttons in general makes their click states pretty finicky. The solution I came up with for this was to have the animating element just be a UIView with all the button styles. Before the UIView animates I add a real button to the view above the UIView with a clear background to the location I want the user interaction to occur. Definitely adds an extra step but is very dependable.

//create button and view
UIButton *viewNotificationBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, ([UIScreen mainScreen].bounds.size.height -70), 320, 50)];
UIView *viewNotificationView = [[UIView alloc] initWithFrame:CGRectMake(0, ([UIScreen mainScreen].bounds.size.height), 320, 50)];

//add elements to screen
[self.view addSubview:viewNotificationView];
[self.view insertSubview:viewNotificationBtn aboveSubview:viewNotificationView];


[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
    [viewNotificationView setFrame:CGRectMake(0, ([UIScreen mainScreen].bounds.size.height - 70), 320, 50)];
} completion:^(BOOL finished) {
    [UIView animateWithDuration:0.5 delay:10.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
        viewNotificationView.alpha = 0;
    } completion:^(BOOL finished) {
        [viewNotificationView removeFromSuperview];
        [viewNotificationBtn removeFromSuperview];
    }];
}];
や三分注定 2024-12-17 03:40:41

使用 animateWithDuration:delay:options:animations:completion: 方法并将 UIViewAnimationOptionAllowUserInteraction 设置为选项

Use animateWithDuration:delay:options:animations:completion: method and set UIViewAnimationOptionAllowUserInteraction as option

清旖 2024-12-17 03:40:41

我最终没有这样做。您无法触摸仍在动画的视图。这就是我的结论。不过,很高兴听到对此的一些想法。

I ended up not doing this. You can't touch a view that still animating. that's my conclusion. Would be happy to hear some thought on this though.

狂之美人 2024-12-17 03:40:41

这不是发生在标准 iOS 应用程序上的事情吗,比如“设置后退按钮”、“菜单滑动”。在动画播放时无法触摸屏幕,而在 Android 中可以触摸。

Isn’t it something that happens on standart iOS apps like Settings back button, menu swipe. There is no way to touch to screen while it is animating whereas you can touch in Android.

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