Domanda

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!

È stato utile?

Soluzione

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];
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top