Detect touch location on UIScrollView ?
Asked Answered
C

2

11

I would like to detect the (initial) touch position in my UIScrollView when the user starts dragging. I have googled this issue and many seem to struggle with this very issue. Now, while I still can't wrap my head around why Apple would not let users access touch information in a scroll view, I can't help but find a solution by myself. However all my tries failed, so I would like to ask you.

Here is what I thought would work:

I set up a UIPanGestureRecognizer like this in my UIScrollView subclass and add it to its gesture recognizers:

UIPanGestureRecognizer *tapDrag = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(touchedAndDragged:)];
tapDrag.cancelsTouchesInView = NO;
tapDrag.delegate = self;
[self addGestureRecognizer:tapDrag];  

And the corresponding method:

-(void)touchedAndDragged:(UIPanGestureRecognizer*)t{

     CGPoint loc = [t locationInView:self];
     //do something with location (that is exactly what I need)
     //...

     //Now DISABLE and forward touches to scroll view, so that it scrolls normally
     t.enabled = NO;
     /****
     ?????
     *****/

}

As indicated by the comments, I would like to disable the pan gesture after I have the point and then disable the recognizer(while STILL dragging!) and "pass" the touches to my scroll view, so that the user can scroll normally. Is that feasible at all? Is there any other solution to it ?

Chloroplast answered 27/6, 2013 at 13:31 Comment(0)
D
34

Well UIScrollView's already have a built in pan gesture that you could tap into. Usage would be as simple as setting your class as your scroll view's delegate (to utilize scrollViewWillBeginDragging) and using UIPanGestureRecognizer's -locationInView: to determine touch location.

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    CGPoint location = [scrollView.panGestureRecognizer locationInView:scrollView];
    NSLog(@"%@",NSStringFromCGPoint(location));
}
Disengagement answered 27/6, 2013 at 14:15 Comment(1)
is it possible to detect if this scroll Is at some xx location then scrollView scrolling or touch will not be triggered? let's say i have two views, map and a panel below map in a scroll view, i don't want scroll view to fire its delegate when i touch map and map should get its touchesMoravian
D
0

Why don't you grab the start location in -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event that will be more convenient and efficient.

Doall answered 27/6, 2013 at 13:39 Comment(2)
I am fully aware of the touch functions and in fact, I tried to implement them, but they do not get called at all.Chloroplast
Not with User Interaction Enabled. However disable it and it will. But then you wont be able to interact with any of its childobjects...Radiobroadcast

© 2022 - 2024 — McMap. All rights reserved.