3

I am trying to design a view which contains a list of items to be ordered and placed into an array. The items are dynamically generated and thus cold be an amount that goes off the bottom of the screen.

For this reason my first view is a UIScrollview which takes the whole devices screen Nested under this I have a label, explaining what the list is for and how to interact with it and then a UITableView with drag & drop with the delegate methods from http://b2cloud.com.au/how-to-guides/reordering-a-uitableviewcell-from-any-touch-point

The problem I am facing is that while the script works great when there are 1 or two rows, when the contentsize of the UIscrollview is larger than the screen it seems to take priority over the drag & drop leading to unpredictable behaviour.

Is there any way to make clicks on the table take priority to only edit the cells and allow the user to scroll by interacting elsewhere on the view?

Thanks

UPDATE

Based on the comment below I managed to get:

- (void)viewDidLoad
{
    [super viewDidLoad];
//
//
//

UIPanGestureRecognizer *tapGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(wasPanned:)];
    [self.view addGestureRecognizer:tapGesture];
}
-(void)wasPanned:(UIPanGestureRecognizer *)gesture
{
    CGPoint point = [gesture locationInView:scrollView];
    UIView *isTable = [scrollView hitTest:point withEvent:nil];
    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        if([[[isTable class] description] isEqualToString:@"UITableViewCellReorderControl"])
        {
            NSLog(@"Dragged from within table");
            [scrollView setScrollEnabled: NO];

        }
        else
        {
            [scrollView setScrollEnabled:YES];
        }
    }
    else{
        [scrollView setScrollEnabled:YES];
    }
}

Now, when the scrollview is not long enough to begin scrolling it NSLogs the message fine However when the scrollview is longer it only recognises the gesture if the scrollview hasn't began scrolling first

Update

I now have the Console recognising touches in the table 100% of the time and disable scrolling. Disabling the scrolling however also stops the drag & drop functionality. Does anyone know why?

Extra code:

tapGesture.delegate = self;

#pragma mark UIGestureRecognizerDelegate
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
Stephen Groom
  • 3,887
  • 1
  • 15
  • 23
  • Hey dude have looked at, http://stackoverflow.com/questions/9715582/iphone-ios-how-to-implement-drag-and-drop-for-subviews-of-a-scrollview. – BooRanger Jul 04 '13 at 09:34
  • I had read some similar articles, and the concensus is that I need to be cancelling or disabling the scroll behaviour whenever the tableview is interacted with. I had tried setting canCancelContentTouches = No but the main issue is that the tableview isn't registering any interaction before the scroll starts... I will try some other methods/listeners and report back :) – Stephen Groom Jul 04 '13 at 09:49

1 Answers1

0

So my final (but not perfect) solution was to do this:

.h

<UIGestureRecognizerDelegate>

.m (viewDidLoad)

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(wasTapped:)];
    [self.view addGestureRecognizer:tapGesture];
    tapGesture.delegate = self;

.m

-(void)wasTapped:(UIPanGestureRecognizer *)gesture
{
    CGPoint point = [gesture locationInView:scrollView];
    UIView *isTable = [scrollView hitTest:point withEvent:nil];
    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        if([[[isTable class] description] isEqualToString:@"UITableViewCellReorderControl"])
        {
            NSLog(@"Dragged from within table");
            [scrollView setScrollEnabled: NO];

        }
        else
        {
            [scrollView setScrollEnabled:YES];
        }
    }
    else{
        [scrollView setScrollEnabled:YES];
    }
}

A tap gesture works better than a Pan gesture because a pan gesture seems to recognise press&hold, drag, stop, drag as two different gestures. The feature now works as it should but if you so much as move your finger before the animation for a cell moving has started it will scroll. You also have to wait for the view (can I call it overscroll?) animation to completely stop and the scrollbars disappear before it will grab cells.

I'd appreciate it if anyone can improve on it :)

Stephen Groom
  • 3,887
  • 1
  • 15
  • 23