7

I have some UIButtons within a UIScrollView, but I do not want to delay the button touches. However, as soon as the scroll view detects a drag/scroll, I want to cancel the UIButton touch and proceed with the scrolling of the UIScrollView.

I have included the following...

_scrollView.delaysContentTouches = NO;

...but (obviously) the button touch does not cancel when dragged. Does anyone have any suggestions as to how I can implement this functionality?

lucien
  • 197
  • 2
  • 8

2 Answers2

11

You can override this method of UIScrollView.

-(BOOL)touchesShouldCancelInContentView:(UIView *)view
{   
    return YES;
}

And in your UIButton, you should add different method for different control events.

  1. show highlight effect for UIControlEventTouchDown.
  2. trigger button for UIControlEventTouchUpInside | UIControlEventTouchUpOutside;
  3. clear highlight effect for UIControlEventTouchCancel.
Streeter
  • 556
  • 4
  • 22
wods
  • 126
  • 1
  • 4
1

You could use scrollViewWillBeginDragging to fire off a notification and handle the button canceling by listening for it in your buttons' code. I think this is what you are trying to do, but I'm not sure if I have understood your question correctly.

Psiticosis
  • 118
  • 8
  • Thanks for your feedback. I tried scrollViewWillBeginDragging but it doesn't fire if you tap a button before scrolling. What I want is to tap a button, but have no delay. Then, if you move your finger while it is still down, it cancels the button and starts scrolling. – lucien Apr 04 '13 at 20:18
  • Try this on the iOS home screen... tap on an app, and you'll notice that it darkens, but as soon as you drag your finger it cancels the touch and scrolls instead – lucien Apr 04 '13 at 20:19
  • So you want the button to immediately fire off an action but when the scroll view is dragged to cancel out? And once you press a button, then drag, scrollViewWillBeginDragging does not get called? Good example with the homescreen. – Psiticosis Apr 04 '13 at 20:52
  • Does scrollViewDidScroll get called? – Psiticosis Apr 04 '13 at 21:13
  • scrollViewDidScroll does not get called either. Once you tap on a button, if you drag your finger, it still thinks you're tapping on the button until your finger is pretty far away from the button (standard UIButton behavior) before it cancels – lucien Apr 05 '13 at 13:34