开发者

UIImageView subclass not passing touches to controller subviews

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] ini开发者_运维问答tWithFrame:[[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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜