如何扭转UIView的运动?

时间:2013-07-08 14:00:43

标签: ios objective-c xcode4.5

我有一个uiview,它叠放在另一个uiview的顶部,在左侧来回滑动。如何将其反转,让它在右侧滑动?这是我正在使用的代码:

//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;
                     }];
}

1 个答案:

答案 0 :(得分:1)

 - (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;
                     }];
}

希望这会有所帮助......