hitTest:WithEvent and Subviews

前端 未结 5 1111
悲哀的现实
悲哀的现实 2021-01-02 02:05

I have 2 views , but i want to make 1 view (virtually) bigger. if I place my tapGesture on v1, the tap gesture works with a bigger hit area but if I place my tapGesture on

5条回答
  •  时光取名叫无心
    2021-01-02 02:27

    The way you are doing it is possible but difficult to do correctly.

    What I recommend is to add the UITapGestureRecognizer to their common superview and then decide what view is the receiver depending on tap location, e.g.

    - (void)onTap:(UITapGestureRecognizer*)tapRecognizer {
        CGPoint touchPosition = [tapRecognizer locationInView:self];
    
        CGRect view1Frame = self.view1.frame;
        view1Frame.width += 100;
        view1Frame.height += 100;
    
        if (CGRectContainsPoint(view1Frame, touchPosition)) {
            [self.view1 handleTap];
            return;
        }
    
        ...
    }
    

    If the views don't have a common superview, just embed each of them in a bigger transparent view and add the recognizer to this bigger view.

提交回复
热议问题