I'm trying to replicate the effect done in this gif.

I'm thinking this would be done with UIPanGestureRecognizer, but I'm not sure. Thanks!

有帮助吗?

解决方案

This can be accomplished quite easily with UIPanGestureRecognizer. Use translationInView to find out how much the user's finger moved by, and move your view according to the finger movement. In this example, self refers to the view controller, view1 is the view on top you want to drag.

UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[self.view addGestureRecognizer:pan];

And handle it:

-(void)handlePan:(UIPanGestureRecognizer*)sender
{
    CGFloat xMovement = [sender translationInView:sender.view].x;
    // Do something with the movement
    view1.frame = CGRectOffset(view1.frame, xMovement, 0);

    // Then reset the translation
    [sender setTranslation:CGPointZero inView:sender.view];
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top