質問

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 ?

役に立ちましたか?

解決

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));
}

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top