简体   繁体   中英

Detect touch on uiview under the UIButton when button is pressed

i am using a uiview parent view....overidded touch began and touch ended methods... when i added a UIButton as a subview in the view and touch on the button touchdown event is detected and associated method is called ...but touchbegan method that was over ridden is not called...

what i want is when i touch the button the method associated with the touchdown event and touchBegan method of uiview both be called simultaneously...UIbutton is not passing the touch to its superview ie uiview.....? Any idea how to do that ? Thanks

I'm not sure exactly how to call two touchesBegan events simultaneously on two different views, but you probably want to override the hitTest:withEvent: method of UIView . This will allow you to catch a touch event on the view underneath the UIButton before it gets to the button (hitTests work from the window upwards to the foremost view).

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if (passToSubviews)
    {
        return [super hitTest: point withEvent: event];
    }

    // otherwise stop the subview receiving touches
    if ([super hitTest: point withEvent: event])
    {
        return self;
    }

    return nil;     
}

Maybe this can help...

EDIT:

Just a guess but you could try:

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView* hitSubview = [super hitTest: point withEvent: event];
    if (hitSubview)
    {
         [self doTouchedInStuff];
         return hitSubview;
    }

    return self;         
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self doTouchedInStuff];

    [super touchesBegan:touches withEvent:event];
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM