Passing through touches to UIViews underneath

前端 未结 5 1375
庸人自扰
庸人自扰 2020-12-01 03:17

I have a UIView with 4 buttons on it and another UIView on top of the buttons view. The top most view contains a UIImageView with a <

5条回答
  •  生来不讨喜
    2020-12-01 03:49

    I have a another solution. I have two views, let's call them CustomSubView that were overlapping and they should both receive the touches. So I have a view controller and a custom UIView class, lets call it ViewControllerView that I set in interface builder, then I added the two views that should receive the touches to that view.

    So I intercepted the touches in ViewControllerView by overwriting hitTest:

    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
    {
        return self;
    }
    

    Then I overwrote in ViewControllerView:

    - (void)touchesBegan:(NSSet *)touches
               withEvent:(UIEvent *)event
    {
        [super touchesBegan:touches withEvent:event];
        for (UIView *subview in [self.subviews reverseObjectEnumerator])
        {
            if ([subview isKindOfClass:[CustomSubView class]])
            {
                [subview touchesBegan:touches withEvent:event];
            }
        }
    }
    

    Do the exact same with touchesMoved touchesEnded and touchesCancelled.

提交回复
热议问题