UIImageView 子类不将触摸传递给控制器​​子视图

发布于 2024-09-11 11:18:17 字数 2547 浏览 3 评论 0原文

当我在几个 UIButton 子类实例后面添加一个 UIImageView 子类(全屏)时,这些按钮停止接收触摸并且不再起作用。我尝试过的任何方法都无法恢复其与用户触摸的连接。

我已经尝试过:
a) 使用 UIImageView 子类作为前视图(接收触摸)并确保 setUserInteractionEnabled 为 YES。结果:当 UIView 存在时,UIButtons 没有响应。

b) 显式地将触摸从前视图传递到视图控制器,希望它能做正确的事情。结果:UIButtons 不响应触摸。

c) 当前面的 UIView 发生任何触摸时,直接在我想要交互的按钮上调用 touchesBegantouchesMovedtouchesEnded 方法已收到。结果:令人惊讶的是,即使我将触摸事件塞进按钮中,按钮也没有响应。

d) 将触摸从前视图传递到 UIViewController,并迭代所有手动分发触摸的子视图。我包含了一个 touchesBegan 方法,展示了我如何尝试执行此操作。当执行时,循环是无限的,它永远不会超过第一个视图(类型为FrameUIView)。

我知道这个问题可能是由于我对正确的视图层次结构或响应者链缺乏了解而导致的。

如果我评论按钮后创建的最后一个视图,那么按钮就可以正常工作。 UIButtons 在这方面有什么特别之处吗?

这是代码。

我有一个 UIViewController 子类,其中:

    - (void)loadView {

        mainAppView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
        self.view = mainAppView;

        frameImageView = [[FrameUIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        frameImageView.image = [UIImage imageNamed:[[[ThemeController sharedInstance] currentTheme] frameImageFilename]];
        [frameImageView setUserInteractionEnabled:YES];
        [mainAppView addSubview:frameImageView];

        zoomInButton = [[CustomUIButton alloc] initWithFrame:CGRectMake(controlsOriginX + zoomWidth + lightWidth, controlsOriginY, zoomWidth, controlsHeight)];
    [zoomInButton setTitle:@"+" forState:UIControlStateNormal];
    [[zoomInButton titleLabel] setFont:[UIFont systemFontOfSize:28]];
    [zoomInButton addTarget:self action:@selector(doMyAction) forControlEvents:UIControlEventTouchDown];
    [zoomInButton addTarget:self action:@selector(cancelMyAction) forControlEvents:UIControlEventTouchUpInside];
    [mainAppView addSubview:zoomInButton];


        FrameFrontUIView *frameFrontImageView = [[FrameFrontUIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        frameFrontImageView.image = [UIImage imageNamed:[[[ThemeController sharedInstance] currentTheme] frameFrontImageFilename]];
        [frameFrontImageView setUserInteractionEnabled:YES];
    [mainAppView addSubview:frameFrontImageView];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UIView *view in self.view.subviews) {
        NSLog(@"view is a %@", [view description]);
        [view touchesBegan:touches withEvent:event];
    }
}

任何有关于如何执行此操作的提示的人都会赢得互联网和 cookie。还接受用于俄罗斯轮盘赌混乱的左轮手枪。谢谢。

When I add a UIImageView subclass (fullscreen) behind a couple of UIButton subclass instances, those buttons stop receiving the touches and don't function anymore. Nothing I have tried has worked to restore their connectivity to user touches.

I have tried:
a) Using a UIImageView subclass for the front view (that receives the touches) and making sure setUserInteractionEnabled is YES. Result: UIButtons are not responding when the UIView is present.

b) Explicitly passing the touches from the front view to the view controller in hopes that it will do the right thing. Result: UIButtons are not responding to the touches.

c) Calling the touchesBegan, touchesMoved and touchesEnded methods directly on the button I want to interact with when any touch on the front UIView is received. Result: Strikingly, the buttons don't respond even when I am cramming the touch events down their throat.

d) Passing the touches from the front view to the UIViewController, and iterating over all subviews handing out the touches manually. I included a touchesBegan method showing how I tried to do this. When executed the loop is infinite, it never progresses past the first view (is of type FrameUIView).

I know this problem is likely the result of my lack of understanding of proper view hierarchy or the responder chain.

if I comment the last view created after the button, then the buttons work fine. Is there something special about UIButtons in this respect?

Here is the code.

I have a UIViewController subclass, in it:

    - (void)loadView {

        mainAppView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
        self.view = mainAppView;

        frameImageView = [[FrameUIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        frameImageView.image = [UIImage imageNamed:[[[ThemeController sharedInstance] currentTheme] frameImageFilename]];
        [frameImageView setUserInteractionEnabled:YES];
        [mainAppView addSubview:frameImageView];

        zoomInButton = [[CustomUIButton alloc] initWithFrame:CGRectMake(controlsOriginX + zoomWidth + lightWidth, controlsOriginY, zoomWidth, controlsHeight)];
    [zoomInButton setTitle:@"+" forState:UIControlStateNormal];
    [[zoomInButton titleLabel] setFont:[UIFont systemFontOfSize:28]];
    [zoomInButton addTarget:self action:@selector(doMyAction) forControlEvents:UIControlEventTouchDown];
    [zoomInButton addTarget:self action:@selector(cancelMyAction) forControlEvents:UIControlEventTouchUpInside];
    [mainAppView addSubview:zoomInButton];


        FrameFrontUIView *frameFrontImageView = [[FrameFrontUIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        frameFrontImageView.image = [UIImage imageNamed:[[[ThemeController sharedInstance] currentTheme] frameFrontImageFilename]];
        [frameFrontImageView setUserInteractionEnabled:YES];
    [mainAppView addSubview:frameFrontImageView];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UIView *view in self.view.subviews) {
        NSLog(@"view is a %@", [view description]);
        [view touchesBegan:touches withEvent:event];
    }
}

Anyone with a tip on how to do this wins the internets and a cookie. Also accepting revolvers for russian roulette mayhem. Thank you.

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

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

发布评论

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

评论(2

扭转时空 2024-09-18 11:18:17

您是否尝试过使用没有图像视图的按钮?不建议对 UIButton 进行子类化。实例化 UIButtons 的方法是使用工厂方法:

UIButton* myButton = [UIButton buttonWithType:UIButtonTypeCustom];

所以我的猜测是您创建的 CustomUIButton 实例没有正确设置。

Have you tried using the buttons without the image view behind them? Subclassing UIButton is not advisable. The way to instantiate UIButtons is with factory methods:

UIButton* myButton = [UIButton buttonWithType:UIButtonTypeCustom];

So my guess is that the CustomUIButton instances you have created are not properly set up.

回梦 2024-09-18 11:18:17

这个问题很老了,但我刚刚遇到了同样的问题。

我使用的解决方案是创建一个容器 UIView,然后将 UIImageView 添加为子视图,然后在其上添加按钮、文本框等。看起来效果不错。

我真的不明白为什么使用 UIImageView 作为按钮的超级视图会出现问题。

This question is old, but I just ran into the same problem.

The solution I used is to create a container UIView, then add the UIImageView as a subview, then add the buttons, text boxes, etc. over that. Seems to work well.

I don't really understand why this is an issue with using a UIImageView as the superview for buttons.

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