UIPanGestureRecognizer最大化和最小化UIView

时间:2012-12-25 16:32:00

标签: ios uiview uipangesturerecognizer

为了最小化和最大化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;
    }
}

但是,转换非常快,因此我移动了几个像素,它覆盖了整个屏幕。我无法弄清楚我的代码需要哪些更正。

1 个答案:

答案 0 :(得分: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,因为你似乎没有使用它。我还将newPoint重命名为translation,因为它不是屏幕上的一个点,而是衡量您的手指在屏幕上移动(或翻译)的程度。我还将oldRect重命名为newFrame,因为我认为更准确地捕获它是什么。

基本上,我试图保留你的例程的逻辑,但只是澄清你的逻辑和变量名称。我原以为你可能想要一个额外的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];
    }
}