0
votes

I have a UIScrollView which scrolls horizontally. I have also put my own handler to do something different on a vertically scroll. This works really well but then I have added some buttons onto the UIScrollView using the following code:

UIButton *catButton = [UIButton buttonWithType:UIButtonTypeCustom];
[catButton addTarget:self action:@selector(clickCatButton:) forControlEvents: UIControlEventTouchUpInside];

the button's action gets called when clicked:

- (void) clickCatButton:(id) sender 
{
   [NSObject cancelPreviousPerformRequestsWithTarget:self];
}

but my problem is that since putting the buttons on the UIScrollView they take over the code that manages the vertical scroll.

I'm a bit stuck on what the best approach is to fix this.

Thanks for your help

1

1 Answers

1
votes

If you really want to handle vertical scroll even if dragging happens on the buttons, what you might do is to subclass UIButton and forward touch events to the next responder (which in this case is the scroll view). In your UIButton subclass, override the event handling methods (touchesBegan... etc.) to call the superclass implementation (UIButton's), then forward the events to the next responder, for example:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
    [super touchesBegan:touches withEvent:event];
    [self.nextResponder touchesBegan:touches withEvent:event]; 
}

But be careful and think of all the cases that could happen when the button's event handling and your handling for vertical scroll go together. For example, if dragging goes across a button (well, although it wouldn't happen if the vertical scroll works appropriately), then the button thinks it's a valid 'pressing' on the button and it will fire an action message.