2

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

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

b) 明确地将触摸从前视图传递给视图控制器,希望它会做正确的事情。结果:UIButtons对触摸没有反应。

c)当收到正面的任何触摸时,直接在我想与之交互的按钮上调用touchesBegan,touchesMoved和方法。结果:引人注目的是,即使我将触摸事件塞进它们的喉咙,按钮也没有响应。touchesEndedUIView

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。还接受俄罗斯轮盘赌混乱的左轮手枪。谢谢你。

4

2 回答 2

0

您是否尝试过使用不带图像视图的按钮?子类化UIButton是不可取的。实例化的UIButtons方法是使用工厂方法:

UIButton* myButton = [UIButton buttonWithType:UIButtonTypeCustom];

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

于 2010-07-26T23:35:43.393 回答
0

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

我使用的解决方案是创建一个容器 UIView,然后将 UIImageView 添加为子视图,然后在其上添加按钮、文本框等。似乎运作良好。

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

于 2011-10-20T00:48:00.450 回答