Search code examples
iosobjective-cxcode4.5

How to reverse movement of a UIView?


I have a uiview that is stacked on top of another uiview that slides back and forth on the left side. How can I reverse the it, to have it slide on the right side? Here is the code that I am using:

//Slides Left
- (IBAction) panLayer:(UIPanGestureRecognizer *)pan {

    if (pan.state == UIGestureRecognizerStateChanged) {

        CGPoint point = [pan translationInView:self.topLayer];
        CGRect frame = self.topLayer.frame;
        frame.origin.x = self.layerPosition +point.x;
        if (frame.origin.x < 0) frame.origin.x = 0;
        self.topLayer.frame = frame;

    }

    if (pan.state ==UIGestureRecognizerStateEnded) {
        if (self.topLayer.frame.origin.x <= 160) {
            [self animateLayerToPoint:0];

        } else {
            [self animateLayerToPoint:VIEW_HIDDEN];
        }

    }

}
// Slides right
- (IBAction)panLayerRight:(UIPanGestureRecognizer *)pan2
{
    if(pan2.state == UIGestureRecognizerStateChanged)
    {
        CGPoint point = [pan2 translationInView:topLayer];
        CGRect frame = topLayer.frame;
        frame.origin.x = layerPosition + point.x;
        if(frame.origin.x + 320 > 320) frame.origin.x = 0;
        topLayer.frame = frame;
    }

    if (pan2.state == UIGestureRecognizerStateEnded) {
        if (topLayer.frame.origin.x +320 <= 260) {
            [self animateLayerToPoint2:-180];
        }
        else
        {
            [self animateLayerToPoint2:VIEW_HIDDEN];
        }
    }
}

-(void) animateLayerToPoint:(CGFloat)x
{
    [UIView animateWithDuration:0.3
                          delay:0
                        options:UIViewAnimationCurveEaseOut
                     animations:^{
                         CGRect frame = self.topLayer.frame;
                         frame.origin.x = x;
                         self.topLayer.frame = frame;
                     }
                     completion:^(BOOL finished){
                         self.layerPosition = self.topLayer.frame.origin.x;
                     }];

}

-(void)animateLayerToPoint2:(CGFloat)x
{
    [UIView animateWithDuration:0.3
                          delay:0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         CGRect frame = topLayer.frame;
                         frame.origin.x = x;
                         topLayer.frame = frame;
                     }completion:^(BOOL finished) {
                         self.layerPosition = topLayer.frame.origin.x;
                     }];
}

Solution

  •  - (IBAction)panLayer:(UIPanGestureRecognizer *)pan
        {
            if(pan.state == UIGestureRecognizerStateChanged)
            {
                CGPoint point = [pan translationInView:topLayer];
                CGRect frame = topLayer.frame;
                frame.origin.x = layerPosition + point.x;
                if(frame.origin.x + 320 > 320) frame.origin.x = 0;
                topLayer.frame = frame;
            }
    
        if (pan.state == UIGestureRecognizerStateEnded) {
            if (topLayer.frame.origin.x +320 <= 260) {
                [self animateLayerToPoint:-180];
            }
            else
            {
                [self animateLayerToPoint:0];
            }
        }
    }
    
    -(void)animateLayerToPoint:(CGFloat)x
    {
        [UIView animateWithDuration:0.3
                              delay:0
                            options:UIViewAnimationOptionCurveEaseOut
                         animations:^{
                             CGRect frame = topLayer.frame;
                             frame.origin.x = x;
                             topLayer.frame = frame;
                         }completion:^(BOOL finished) {
                             self.layerPosition = topLayer.frame.origin.x;
                         }];
    }
    

    Hope this helps...