Touch event handled by multiple views

后端 未结 3 1270
挽巷
挽巷 2021-02-06 03:39

I have a subclass of UIView on top of a UITableView. I am using the UITableView to display some data and, at the same time, I would like t

3条回答
  •  渐次进展
    2021-02-06 04:18

    The way I have solved this problem is in a way that is not that clean, but it works. Please let me know if there's a better way to do this.

    I have overridden hitTest for my custom UIView so that it directs touches to the UITableView underneath. Then in the UITableView I am handling the gestures through touchesBegan, touchesMoved, etc. There I am also calling touchesBegan on the UIView.

    In this way touches are handled by two views. The reason why I am not doing the other way around (having UIView's touchesBegan calling UITableView's touchesBegan) is that gestures recognizers on the UITableView would not work.

    UIView subclass' hitTest

    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
    {
        // tview is the UITableView subclass instance
        CGPoint tViewHit = [tView convertPoint:point fromView:self];        
        if ([tView pointInside:tViewHit withEvent:event]) return tView;
    
        return [super hitTest:point withEvent:event];
    }
    

    UITableView subclass's touchesBegan

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
        CGPoint location = [touch locationInView:touch.view];
        // ....
    
        // view is the UIView's subclass instance
        [view touchesBegan:touches withEvent:event];
    }
    

提交回复
热议问题