2

为了最小化和最大化 UIView,我使用了 UIPangestureRecognizer。代码如下:

-(void) pannningMyView:(UIPanGestureRecognizer*) panGesture{
    CGPoint newPoint=[panGesture translationInView:self.view];
    CGPoint oldPoint=self.myPanningView.frame.origin;
    CGFloat dx=newPoint.x;
    CGFloat dy=newPoint.y;
    if(dx>0){
        CGRect oldRect=self.myPanningView.frame;
        oldRect.size.width+=dx;
        oldRect.size.height+=dy;
        self.myPanningView.frame=oldRect;
    }
}

但是,过渡非常快,以至于我移动了几个像素,它覆盖了整个屏幕。我无法弄清楚我的代码需要进行哪些更正。

4

1 回答 1

1

问题是您的翻译是累积的,因为它translationInView是从连续手势的开头开始的,但是您正在将翻译添加到当前帧,而不是原始帧。这可以通过检查手势状态来解决,如果您处于手势的开头,则保存原始帧,然后在手势进行时将其用作未来翻译的基础。

-(void) panningMyView:(UIPanGestureRecognizer*) panGesture
{
    static CGRect originalFrame; // or you could make this a non-static class ivar

    if (panGesture.state == UIGestureRecognizerStateBegan)
    {
        originalFrame = self.myPanningView.frame;
    }
    else if (panGesture.state == UIGestureRecognizerStateChanged)
    {
        CGPoint translation = [panGesture translationInView:self.view];
        if (translation.x > 0) {
            CGRect newFrame = originalFrame;
            newFrame.size.width += translation.x;
            newFrame.size.height += translation.y;
            self.myPanningView.frame = newFrame;
        }
    }
}

请注意,我摆脱了它,oldPoint因为您似乎没有使用它。我也重命名为newPointtranslation因为它不是屏幕上的一个点,而是衡量您的手指在屏幕上移动(或平移)多少的量度。我也重命名oldRectnewFrame,因为我认为它更准确地捕捉到了它是什么。

本质上,我试图保留您的例程的逻辑,但只是澄清您的逻辑和变量名称。我原以为您可能需要其他else if原因,检查结束或取消的手势,使用动画来完成或适当地反转手势,但我没有解决这个问题,因为您没有在原始问题中引用此问题。

无论如何,我希望你能明白我们在这里做什么。我们正在保存原始帧并将翻译应用于该帧,而不是将其应用于当前帧。


更新:

在后续问题中,您询问了如何澄清动画。您可能会执行以下操作:

-(void) panningMyView:(UIPanGestureRecognizer*) panGesture
{
    static CGRect originalFrame; // or you could make this a non-static class ivar
    CGPoint translation = [panGesture translationInView:self.view];

    if (panGesture.state == UIGestureRecognizerStateBegan)
    {
        originalFrame = self.myPanningView.frame;
    }
    else if (panGesture.state == UIGestureRecognizerStateChanged)
    {
        if (translation.x > 0) {
            CGRect newFrame = originalFrame;
            newFrame.size.width += translation.x;
            newFrame.size.height += translation.y;
            self.myPanningView.frame = newFrame;
        }
    }
    else if (panGesture.state == UIGestureRecognizerStateEnded ||
             panGesture.state == UIGestureRecognizerStateCancelled ||
             panGesture.state == UIGestureRecognizerStateFailed)
    {
        CGRect finalFrame = originalFrame;

        // if we've gone more than half way, move it all the way, 
        // otherwise return it to the original frame

        if (translation.x > (self.view.frame.size.width / 2.0))
        {
            finalFrame.size.width += self.view.frame.size.width;
            finalFrame.size.height += self.view.frame.size.height;
        }

        [UIView animateWithDuration:0.5
                              delay:0.0
                            options:UIViewAnimationOptionCurveEaseOut
                         animations:^{
                                self.myPanningView.frame = finalFrame;
                             }
                         completion:nil];
    }
}
于 2012-12-25T17:54:29.120 回答