UIPanGestureRecognizer at view with UIScrollView

770 views Asked by At

I have a view with UIPanGestureRecognizer.

UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc]
                                          initWithTarget:self action:@selector(handlePan:)];
    [recognizer setMaximumNumberOfTouches:1];
    [recognizer setDelegate:self];

[self.view addGestureRecognizer: recognizer];

=======
-(void) handlePan:(UIPanGestureRecognizer*)gestureRecognizer
{
  move subview of self.view
}

subview have a scroll view inside. how to capture gesture with event -handlePan: when scroll view is at the end of horizontal scrolling?

enter image description here

2

There are 2 answers

0
guang On

I am working with panGesture since last week. It seems to me there are two issues here;

1)UX design. UIScrollView : UIView is "touch" recognizable - (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view;

UIPangestureRecognizer also detects "touch".

- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView*)view; // the location of a particular touch

From a user's experience point of view, how do you indicate the differences between TWO TOUCHES in ONE UI area, purely for the design aspect.

2) Code. It looks like you just want to pan a view (UIScrollView, or other UIView's views)

UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];

// no need to set the number of touches to 1. 1 is default number. 
yourView = [[UIImageView alloc]init]; // or initWithFrame
// add the recognizer to your view 
[youView addGestureRecognizer:recognizer];
//add this view to the mainView
[self addSubview: yourView];
// then process @selector(handlePan:)
-(void) handlePan:(UIPanGestureRecognizer*)gestureRecognizer
{ 
// this method is to deal with location of the panGesture itself, not other third views.
CGPoint touchLocation = [gestureRecognizer locationInView:self]; 
yourView.center = touchLocation;

}

0
Munahil On

You can use following function:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer