仅垂直平移UIView并具有弹性效果?

时间:2014-03-06 01:13:16

标签: ios objective-c uiview uipangesturerecognizer

我的应用中有一个视图占据屏幕的下半部分。我希望允许用户向下拖动视图,使其仅占用屏幕底部的1/4。然后,用户可以将其向上拖动以再次占据屏幕的1/2。就像在iOS7中的底部向上滑动视图(即手电筒,计算器,音量等......)。我需要以完全相同的方式运行,但不要让它一直离开屏幕。

所以我只需要保持平移垂直。当它占据屏幕的一半时,我也希望它停止,然后当它是屏幕的1/4时停止另一个方向。我还希望它在拖过某个点时捕捉到位,这样只有两个位置是1/2和1/4在屏幕上。所以我不希望用户能够在屏幕上拖动它1/3。

这是我到目前为止所拥有的,但它允许我投掷或抛出超过最大停留点的视图。

- (IBAction)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
 {
      UIView *piece = [gestureRecognizer view];

if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
    CGPoint translation = [gestureRecognizer translationInView:[piece superview]];

    CGPoint vel = [gestureRecognizer velocityInView:piece];
    if (vel.y > 0)
    {
        // user dragged towards the right
        //counter++;

        if(piece.frame.origin.y >= (self.view.frame.size.height - piece.frame.size.height) ) {
            // don't update

        } else {
            [piece setCenter:CGPointMake([piece center].x, [piece center].y + translation.y)];
        }
    } else {

        // user dragged towards the left
        //counter--;
        NSLog(@"");
        if(piece.frame.origin.y <= 205) {
            // don't update

        } else {

            [piece setCenter:CGPointMake([piece center].x, [piece center].y + translation.y)];
        }
    }

    [gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
}
}

1 个答案:

答案 0 :(得分:2)