Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 548
Nathan Fraenkel Avatar asked Jun 27 '12 23:06

Nathan Fraenkel


People also ask

How can I tell when a UIScrollView has finished scrolling?

scrollViewDidScroll only notifies you that the scroll view did scroll not that it has finished scrolling. The other method scrollViewDidEndScrollingAnimation only seems to fire if you programmatically move the scroll view not if the user scrolls.

How does UIScrollView work?

A floating-point value that determines the rate of deceleration after the user lifts their finger. We can assume that this rate indicates how much the scroll velocity will change after one millisecond (all UIScrollView values are expressed in milliseconds, unlike UIPanGestureRecognizer ).

What is ScrollView iOS?

A view that allows the scrolling and zooming of its contained views. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+


1 Answers

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   
}
like image 68
AJS Avatar answered Oct 02 '22 14:10

AJS