hitTest:WithEvent and Subviews

前端 未结 5 1116
悲哀的现实
悲哀的现实 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条回答
  •  Happy的楠姐
    2021-01-02 02:41

    You need to implement method:

    - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
    {
        CGFloat radius = 100.0;
        CGRect frame = CGRectMake(0, 0,
                                  self.frame.size.width + radius,
                                  self.frame.size.height + radius);
    
        if (CGRectContainsPoint(frame, point)) {
            return YES;
        }
    
        return [super pointInside:point withEvent:event];
    }
    

    And then:

    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
    {
        CGFloat radius = 100.0;
        CGRect frame = CGRectMake(0, 0,
                                  self.frame.size.width + radius,
                                  self.frame.size.height + radius);
    
        if (CGRectContainsPoint(frame, point)) {
            return self;
        }
        return [super hitTest:point withEvent:event;
    }
    

    From documentation

    This method traverses the view hierarchy by sending the pointInside:withEvent: message to each subview to determine which subview should receive a touch event. If pointInside:withEvent: returns YES, then the subview’s hierarchy is traversed; otherwise, its branch of the view hierarchy is ignored. You rarely need to call this method yourself, but you might override it to hide touch events from subviews.

    This method ignores view objects that are hidden, that have disabled user interactions, or have an alpha level less than 0.01. This method does not take the view’s content into account when determining a hit. Thus, a view can still be returned even if the specified point is in a transparent portion of that view’s content.

提交回复
热议问题