Question

Working with XCode 4.2 and trying to get to grips with the UIGestureRecognisers. All seems to be going fairly well so far, but am still having a few issues.

When I was using the Swipe gesture recognisers, everything was fine, it would recognise swipes in all different directions and would do so continuously. My problem now is, when using the pan gesture recogniser, it recognises the first pan swipe fine, but then just refuses to accept any further gestures. So I can move items about once as needed, but after that, can do nothing.

I set my gesture up as follows:

UIPanGestureRecognizer *panBody = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panBody:)];
[bodyGestureView addGestureRecognizer:panBody];

Then this is my 'panBody' method that handles it all:

- (void)panBody:(UIPanGestureRecognizer *)recognizer
{
CGPoint translate = [recognizer translationInView:self.view];

CGRect bodyPanelFrame = bodyPanel.frame;
bodyPanelFrame.origin.x += translate.x;
bodyPanelFrame.origin.y += translate.y;
recognizer.view.frame = bodyPanelFrame;

CGRect topPanelFrame = topPanel.frame;
topPanelFrame.origin.x += translate.x;
topPanelFrame.origin.y += translate.y;
recognizer.view.frame = topPanelFrame;

CGRect sidePanelFrame = sidePanel.frame;
sidePanelFrame.origin.x += translate.x;
sidePanelFrame.origin.y += translate.y;
recognizer.view.frame = sidePanelFrame;

NSLog(@"Panning");

if (recognizer.state == UIGestureRecognizerStateEnded)
{
    bodyPanel.frame = bodyPanelFrame;

    if((topPanel.frame.origin.x + translate.x) <= 193)
    {
        topPanel.frame = CGRectMake(topPanelFrame.origin.x, topPanel.frame.origin.y, topPanel.frame.size.width, topPanel.frame.size.height);
    }
    else
    {
        topPanel.frame = CGRectMake(193, 0, topPanel.frame.size.width, topPanel.frame.size.height);
        NSLog(@"Top panel not in frame");
    }

    if((sidePanel.frame.origin.y + translate.y) < 57)
    {
        sidePanel.frame = CGRectMake(sidePanel.frame.origin.x, sidePanelFrame.origin.y, sidePanel.frame.size.width, sidePanel.frame.size.height);
    }
    else
    {
        sidePanel.frame = CGRectMake(0, 56, sidePanel.frame.size.width, sidePanel.frame.size.height);
        NSLog(@"Side panel not in frame");
    }
}
}

bodyPanel, topPanel and sidePanel are IBOutlets linked to UIView's overlayed across the top of my interface .xib

If anybody could shed any light on this information, that would be great cause I have absolutely no idea what is going on!!

Thanks,

Matt

Was it helpful?

Solution

First I would check that

if (recognizer.state == UIGestureRecognizerStateChanged)

before doing your translations (there are many other possible states which would not justify you taking any action). Also I would reset the translation at every callback given you are accumulating them using the UIPanGestureRecognizer method

- (void)setTranslation:(CGPoint)translation inView:(UIView *)view

If the gesture recognizer stops it might be that another gesture recognizer is interfering with it. Do you still have an active UISwipeGestureRecognizer there? If so you should probably deactivate one of them. You can also look at this method

- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer

which allows you to specify which recognizer should be given priority.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top