简体   繁体   中英

Swipe UIView off screen

I'd like to be able to have a UIView (or, if necessary, UIViewController), which can be 'dragged'/flicked/swiped off screen - but locked to the vertical axis (so the view would go up or down off the screen to show the view below). This is sort of like the Facebook app's web views.

Could this perhaps be achieved with UIGravityBehavior?

Any ideas would be fantastic.

I have made something similar a while ago by using a UIPanGestureRecognizer. Here is how this would work:

float slidingMenuOffsetSum;

    - (void)handleSlidingMenuPan:(UIPanGestureRecognizer *)gestureRecognizer {
        CGPoint translation = [gestureRecognizer translationInView:_slidingMenuView];

        slidingMenuOffsetSum += translation.y;
        CGPoint newSlidingMenuCenter = CGPointMake(_slidingMenuView.center.x, _slidingMenuView.center.y + translation.y);

        if (_slidingMenuView.frame.origin.y <= 512 && _slidingMenuView.frame.origin.y >= 0) {
            _slidingMenuView.center = newSlidingMenuCenter;
        }

        [gestureRecognizer setTranslation:CGPointZero inView:_slidingMenuView];

        if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
            [UIView animateWithDuration:0.3
                animations:^{
                    [_slidingMenuView setUserInteractionEnabled:false];
                    if (slidingMenuOffsetSum > 0) {
                        if (slidingMenuOffsetSum > _slidingMenuView.frame.size.height / 4) {
                            [self setSlidingMenuHidden:false];
                        } else {
                            [self returnSlidingMenuToPosition];
                        }
                    } else {
                        if (slidingMenuOffsetSum < -_slidingMenuView.frame.size.height / 4) {
                            [self setSlidingMenuHidden:true];
                        } else {
                            [self returnSlidingMenuToPosition];
                        }
                    }
                }
                completion:^(BOOL finished) {
                    slidingMenuOffsetSum = 0;
                    [_slidingMenuView setUserInteractionEnabled:true];
                }];
        }
    }

You can adjust the values at which the sliding menu can move, and the thresholds for detecting if the view should go back to its original position, or eg. go off the screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM