Samuli Lehtonen
Samuli Lehtonen

Reputation: 3860

Presenting a view by pulling from the top of the view

So I am planning to do a different way to display history in my iPad app and I think user could pull up the history view from the bottom, should I just place UIView there and add gesture recognizer for it? Is there any "right" way to do this? I want to make the user actually able to drag the view from the bottom. If you don't understand, ask away and I will elaborate.

Upvotes: 1

Views: 890

Answers (1)

adamrothman
adamrothman

Reputation: 956

You have the right idea. You'd use a UIPanGestureRecognizer to update the view's frame. Keep in mind that you'll have to have something for the user to "pull" on visible at all times - I don't think you'll be able to hide the view completely off screen.

Something like this would go in the implementation of the object you choose to handle events from your gesture recognizer (this sample assumes it's your view controller):

- (void)handleDrag:(UIPanGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateChanged ||
        gesture.state == UIGestureRecognizerStateEnded) {
        CGPoint translation = [gesture translationInView:self.view];
        CGRect newFrame = historyView.frame;
        newFrame.origin.x = newFrame.origin.x + translation.x;
        newFrame.origin.y = newFrame.origin.y + translation.y;
        historyView.frame = newFrame;

        // you need to reset this to zero each time
        // or the effect stacks and that's not what you want
        [gesture setTranslation:CGPointZero inView:self.view];
    }
}

Upvotes: 2

Related Questions