Proud Member
Proud Member

Reputation: 40496

How to cancel touches exactly like UIScrollView?

I created an app where you can drag views around. I needed special behavior that UIScrollView was not a good fit for. But now the problem is that some of these views respond to touch events and when I drag them, I must cancel touches.

UIScrollView has the ability to cancel touches in it's subviews so that when the user touches down on a button and then begins scrolling, the scroll view would cancel the touches in the button as soon as the user scrolled enough, so that the button does not trigger an action.

One way to do it would be to subclass each and every subview and work with boolean flags in all touch handling methods but that would be very tedious and dirty.

Is there a better way through the API to cancel touches in subviews just like UIScrollView does?

Upvotes: 2

Views: 1077

Answers (1)

Kyle Howells
Kyle Howells

Reputation: 3553

It cancels the touches through the UIGestureRecognizer API. UIScrollView actually uses UIPanGestureRecognizer underneath. http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIGestureRecognizer_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40009279-CH1-SW9

UIGestureRecognizer also has the ability to delay touches start being sent, and touches ended.

@property(nonatomic) BOOL delaysTouchesBegan
@property(nonatomic) BOOL delaysTouchesEnded

To perform dragging gestures I use UIPanGestureRecognizer now, like UIScrollView. Doing this will let you have all it's little tricks.

Upvotes: 2

Related Questions