2

I want to implement the functionality of pan gesture to a imageview, this imageview is a subview of a scroll view. the problem is when i try to scroll the scrollview the pan gesture of the imageview gets recognized, and the scrollview does not scroll. please help me with this.

this is the code for initializing the pan to the uiimageview:

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
            [panRecognizer setMinimumNumberOfTouches:1];
            [panRecognizer setMaximumNumberOfTouches:1];
            [panRecognizer setDelegate:self];
            [[[scrollView subviews] objectAtIndex:i] addGestureRecognizer:panRecognizer];

the imageview has the user set to "YES"

and the action for it

-(void)move:(UIPanGestureRecognizer *)sender
{
    [[[[[sender view] superview] superview] superview]  bringSubviewToFront:sender.view];


    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {

        if ([[[sender view] superview] superview] == itemView) // adds the imageview in the item scroll
        {
            int tag = [[sender view] tag] / 100;
            UIScrollView *tempScroll = (UIScrollView *)[itemView viewWithTag:-tag];
            CGRect frame = CGRectFromString([itemFrameDict objectForKey:[NSString stringWithFormat:@"%i",[[sender view] tag]]]);

            CGAffineTransform transform = temp.transform;
            UIImageView *tempImageView =  [[UIImageView alloc] initWithImage: [(UIImageView *) [sender view] image]];
            tempImageView.tag = [[sender view] tag];
            //NSLog(@"[sender view] superview] : %@", [[sender view] superview]);
            tempImageView.frame = frame;
            [tempImageView setTransform:transform];
            [tempImageView setFrame:frame];
            [tempImageView setUserInteractionEnabled:YES];
            [tempScroll addSubview:tempImageView];

            UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
            [panRecognizer setMinimumNumberOfTouches:1];
            [panRecognizer setMaximumNumberOfTouches:1];
            [panRecognizer setDelegate:self];
            [tempImageView addGestureRecognizer:panRecognizer];

            UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
            [singleTapGestureRecognizer setNumberOfTapsRequired:1];
            [tempImageView addGestureRecognizer:singleTapGestureRecognizer];
        }
    }

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateChanged)
    {
        CGPoint translatedPoint = [sender locationInView:[[sender view] superview]] ;
        newTranslatedPoint = [[[sender view] superview] convertPoint:translatedPoint toView:self.view];

        if ([sender view].frame.size.width == 35 && [sender view].frame.size.height == 35)
            [[sender view] sizeToFit];

        [sender view].center =  CGPointMake(newTranslatedPoint.x, newTranslatedPoint.y); //startFrame;

        [self.view addSubview:[sender view]];
    }

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
    {
        CGPoint newPoint = [[[sender view] superview] convertPoint:newTranslatedPoint toView:itemView];
        if (newPoint.y < 0)// || !viewUp)
        {
            //NSLog(@" baseView");
            [baseView addSubview:[sender view]];

            UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
            [doubleTapGestureRecognizer setNumberOfTapsRequired:2];
            [[sender view] addGestureRecognizer:doubleTapGestureRecognizer];

            UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
            [pinchRecognizer setDelegate:self];
            [[sender view] addGestureRecognizer:pinchRecognizer];

            UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
            [rotationRecognizer setDelegate:self];
            [[sender view] addGestureRecognizer:rotationRecognizer];
        }

        else if (newPoint.y > 0)// && viewUp )
        {
            //NSLog(@" itemView");
            int tag = [[sender view] tag] / 100;
            UIScrollView *tempScroll = (UIScrollView *)[itemView viewWithTag:-tag];
            CGRect frame = CGRectFromString([itemFrameDict objectForKey:[NSString stringWithFormat:@"%i",[[sender view] tag]]]);

            CGAffineTransform transform = temp.transform;
            [[sender view] setTransform:transform];
            [[sender view] setFrame:frame];
            [tempScroll addSubview:[sender view]];
        }
    }
}
Khushboo
  • 1,208
  • 1
  • 8
  • 19
  • Please show what did you do in the implementation of the pan gesture. – Zen Apr 12 '13 at 06:02
  • Also while using IB, by default, the `UIImageView` is not user interaction enabled. For gesture recognizers on imageview, you'll have to enable that. – Zen Apr 12 '13 at 06:06
  • @zen i have added the code which I am using – Khushboo Apr 12 '13 at 06:17

1 Answers1

3

I'm not sure I understand what you're trying to achieve exactly by using a UIPanGestureRecognizer on a UIImageView that is already in a UIScrollView. But from your description, it sounds like you want the UIScrollView to do it's thing and for the UIPanGestureRecognizer to not block that. I believe you need to implement this delegate method for your UIPanGestureRecognizer:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

Please read the UIGestureRecognizerDelegate Protocol reference in regards to allowing simultaneous gesture recognition. According to the documentation:

The default implementation returns NO—no two gestures can be recognized simultaneously.

/* EDIT */

From UIPanGestureRecognizer - Only vertical or horizontal: Implement just one direction for a UIPanGestureRecognizer. This is for a vertical pan:

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)panGestureRecognizer {
    CGPoint translation = [panGestureRecognizer translationInView:someView];
    return fabs(translation.y) > fabs(translation.x);
}
Community
  • 1
  • 1
DonnaLea
  • 8,643
  • 4
  • 32
  • 32
  • actually i am implementing drag and drop functionality here – Khushboo Apr 12 '13 at 07:03
  • i have already implemented this - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } – Khushboo Apr 12 '13 at 07:04
  • @Khushboo so you're trying to move the UIImageView around within the UIScrollView? Is the UIScrollView only to be scrolled vertically/horizontally, or both? – DonnaLea Apr 12 '13 at 07:09
  • @Khushboo have you set the contentSize for the UIScrollView correctly? Does scrolling work the way you want it to work if you don't add the panRecognizer? Could you provide more context of your setup, it looks like you're in a for loop given the reference to i. – DonnaLea Apr 12 '13 at 07:13
  • I have a scrollview which scrolls vertically only and have around 20 imageview in it, I have given pan gesture to all these 20 imageviews so that I can drag them to other view, but the problem is when I am scrolling the scrollview, while touching the imageview the pan gesture gets recognised and the imageview gets drag instead of the scrollview scrolling, but when I scroll the scrollview while touching the blank area between the 2 imageviews the scrollview scrolls perfectly – Khushboo Apr 12 '13 at 07:25
  • so you only want the gesture to work horizontally since the scrolling happens vertically? – DonnaLea Apr 12 '13 at 07:45
  • I want if the user pans the imageview horizontally than panGesture should work and if horizontally then the scrollview should scroll – Khushboo Apr 12 '13 at 07:49
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28085/discussion-between-khushboo-and-donnalea) – Khushboo Apr 12 '13 at 07:49
  • 1
    This could be helpful: http://stackoverflow.com/questions/7100884/uipangesturerecognizer-only-vertical-or-horizontal – DonnaLea Apr 12 '13 at 08:34
  • @Khushboo glad to hear it. Which bit in particular did the trick and I can edit my answer more specifically. – DonnaLea Apr 15 '13 at 10:54