UIScrollView prevents touchesBegan, touchesMoved, touchesEnded on view controller

前端 未结 5 1906
一整个雨季
一整个雨季 2020-12-05 07:19

I am handling touches for a couple of my UI components in my view controller (custom subclass of UIViewController). It has methods touchesBegan:withEvent:,

5条回答
  •  独厮守ぢ
    2020-12-05 07:55

    Well this worked, but I'm not sure I can "get away with it", since nextResponder is not one of the UIView methods you're "encouraged" to override in a subclass.

    @interface ResponderRedirectingView : UIView {
    
        IBOutlet UIResponder *newNextResponder;
    
    }
    
    @property (nonatomic, assign) IBOutlet UIResponder *newNextResponder;
    
    @end
    
    
    @implementation ResponderRedirectingView
    
    @synthesize newNextResponder;
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
        }
        return self;
    }
    
    - (UIResponder *)nextResponder {
        return self.newNextResponder;
    }
    
    - (void)dealloc
    {
        [super dealloc];
    }
    
    @end
    

    Then in Interface Builder I made the direct subview of the scroll view one of these, and hooked up its newNextResponder to skip the scrollview and point directly to the view controller.

    This works too, replacing the override of nextResponder with these overrides:

    - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        [self.newNextResponder touchesBegan:touches withEvent:event];
    }
    
    - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        [self.newNextResponder touchesMoved:touches withEvent:event];
    }
    
    - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        [self.newNextResponder touchesEnded:touches withEvent:event];
    }
    
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
        [self.newNextResponder touchesCancelled:touches withEvent:event];
    }
    

提交回复
热议问题