简体   繁体   中英

Using UIPanGestureRecognizer to slide to a new view controller

Im trying to add a welcome screen to my app that you "slide to unlock". The idea is very simple to the Potluck app. Essentially the user would slide the view and as they slide they will see the next view underneath the view they are sliding. When they slide past a certain point it goes to the next view. I'm trying to do this with UIPanGestureRecognizer . I'm assuming I need to find how far the user has dragged out the screen, which I'm not sure how to do. Thanks.

I found the effect Im trying to achieve online in a gif: https://d262ilb51hltx0.cloudfront.net/max/800/1*tKuXa5qf4aSwuSnv05uqxQ.gif

You could use UIDynamicAnimator and UIDynamicBehaviors in order to get a realistic result. Although they're only available for iOS 7.

Set up UIGravityBehavior to pull the view towards the right direction. Then create a UICollisionBehavior to stop the top view falling off the screen. When the pan gesture starts, remove the gravity and attach a UIAttachmentBehavior to the center of the top view. As the pan moves, move the attachment to drag the top view. When the pan gesture is released use the UIPanGestureRecognizer to get the velocity in the view. Give the view a push in the direction of that velocity using UIPushBehavior. Then reapply the gravity in the appropriate direction.

Hope that helps!

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