objective-cuiviewcenteruipangesturerecognizerxcode4.4

Snapping a UIView to a set position after panGesture release


I am playing with XCode at the moment, but have no experience with Objective-C / Cocoa Touch etc so bear with me if this is a terribly rudimentary question. I only ask you guys because I've been trying to Google an answer for days, and am getting bored of dead ends.

I am trying to create a horizontally panning menu.

I am currently holding 3 UILabels inside a master UIView object, which is attached to a UIPanGestureRecognizer.

I have used a handlePan method to receive and translate the *menuview UIView object according to the user's pan gesture, but I need to find the UIView's center coordinates when the PanGesture is released to calculate where to snap the UIView element back/forward to.

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 150);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

if(recognizer.state == UIGestureRecognizerStateEnded)
{
    if(recognizer.view.center.x >= 576);{
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        menuview.transform = CGAffineTransformMakeTranslation((764 - menuview.center.x), 0);
        [UIView commitAnimations];

    }

    if(recognizer.view.center.x && menuview.center.x >= 264);{
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        menuview.transform = CGAffineTransformMakeTranslation((406 - menuview.center.x), 0);
        [UIView commitAnimations];

    }

    if(recognizer.view.center.x < 264);{
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        menuview.transform = CGAffineTransformMakeTranslation((122 - menuview.center.x), 0);
        [UIView commitAnimations];

    }

}

}

You see I was trying to snap to one of three points on the x-axis, depending on where the center of the UIView is when the user releases their PanGesture.

At the moment, it is acting as if it's ignoring the "recognizer.view.center.x" if statements, as no matter where it is released it snaps to x=122 (122 - menuview.center.x).

I have tried these if statements with:

if(menuview.center.x >= 576);{... 

etc, but that also fails.

What do I need to be putting in these if statements to create actions based ont he UIViews CURRENT position when the user releases their PanGesture?

Thanks,

Chris


Solution

  • This is what I use, hopefully it will help you:

    .h

    #import <QuartzCore/QuartzCore.h>
    

    delcare these two floats

    float firstX;
    float firstY;
    

    .m

    -(void)moveHorizontallyAndVertically:(id)sender {
    [[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
    
    //  [self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
    CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
        firstX = [myView center].x;
        firstY = [myView center].y;
    
    
    }
    
    translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
    
    [myView setCenter:translatedPoint];
    
    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
        CGFloat finalX = translatedPoint.x + (0.0*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
        CGFloat finalY = translatedPoint.y + (0.0*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);
        if(finalX < 0) {
            finalX = 0;
        }
        else if(finalX > 320) {
            finalX = 320;
        }
        if(finalY < 0) {
            finalY = 0;
        }
        else if(finalY > 480) {
            finalY = 480;
        }
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        [myView setCenter:CGPointMake(finalX, finalY)];
        [UIView commitAnimations];
    }
    

    }