ios is it possible for a uiscrollview to detect a single tap by a user while still providing its dragging, scrolling capabilities?

Nathan Fraenkel picture Nathan Fraenkel · Jun 28, 2012 · Viewed 12.1k times · Source

I currently am using a UIScrollView to work as a way of scrolling/ dragging the cursor/ marker of a graph that I am displaying on my app. I use scrollViewDidScroll, etc. to detect the contentoffset's position and manipulate what I display on the screen based on this position.

My question is if it is possible to also detect a user's single tap on the screen and get this tap's position (to then set the contentoffset to this position)?

I have read some tutorials that describe creating a subclass of a uiscrollview, but it seems like these subclasses will ONLY account for a single tap and not drags/scrolls also.

Does anyone have any insight on how I might be able to detect a user's single tap on my UIScrollView while still having its scrolling/ dragging capabilities?
Thank you in advance!

Answer

AJS picture AJS · Jun 28, 2012

Use the following code for your scrollView object :

UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
singleTapGestureRecognizer.numberOfTapsRequired = 1;
singleTapGestureRecognizer.enabled = YES;
singleTapGestureRecognizer.cancelsTouchesInView = NO;
[scrollView addGestureRecognizer:singleTapGestureRecognizer];
//[singleTapGestureRecognizer release]; Not needed in ARC-enabled Project

}

- (void)singleTap:(UITapGestureRecognizer *)gesture {  
//handle taps   
}