Breaking News
 |  | 

General Info

How to create Facebook like Swipe, PullToDismiss.

“At the end of the day is how the user remembers the experience. Focus on key experiences.”
?—?Shawn Borsky

I have been great in love with gesture-based apps, One of my favorites was “Paper by Facebook” which was available only in US, Where everything was operated with gestures but eventually Facebook pulled down that app. Facebook has learned a lot from “Paper” experience and they have put a lot of features in their existing Facebook app from it.

We all use Facebook app and we love it. From the user point of view, I love some of the ease gesture like when we go to any post to view comments or photos and then we can Swipe Left OR Pull the Post Down or Up to get back to News Feed.

Today we are going to learn how to implement this gesture-based dismissal of UIViewController like Facebook app.

To start with I will create a Demo Project with Master and Detail View where Master has Button “Go To Detail” to navigate to Detail Page and Detail Page is UITableViewController with multiple sections.

To implement the Interactive Gesture dismissal we will take help of UIPanGestureRecognizer for animation, we will use the popular transition framework Hero. Hero is a framework for building iOS view controller transitions. It provides a declarative layer on top of the UIKit’s cumbersome transition APIs?—?making custom transitions an easy task for developers.

Note :-

I recommend readers to check the demo project of Hero, As this project uses this framework heavily. You can check these article on Hero framework to learn how to integrate and use it in your project as we are not covering this part in this article. I am listing some the method call which I will be using from this framework.

  1. self.heroModalAnimationType , .uncover, .cover to set animation of view dismissal.
  2. Hero.shared.update, Hero.shared.apply, Hero.shared.finish, Hero.shared.cancel to translate the view based on pan gesture.

Ok enough talk’s now lets code.

Extension :-

We will use two helper Extension so that it will help us to achieve our goal.

  1. One for detecting the direction of UIPanGestureRecognizer.
  2. Second for detecting whether UIScrollView is at top or bottom

Below is extension for UIPanGestureRecognizer for detecting direction of Pan gesture.

https://medium.com/media/b10ff7347832bad8f512203f4c5a57e5/href

Below is extension for UIScrollView for detecting the UITableView is at bottom or top.

https://medium.com/media/8c065f438de765b59c68134addabf031/href

Detail View :-

Now to start with we will create a variable of UIPanGestureRecognizer and add this gesture to detail view.

// PanGesture Variable
var panGR : UIPanGestureRecognizer!
// Detect current direction.
var progressBool : Bool = false
// Hero dismiss bool to track dismissal
var dismissBool : Bool = true
override func viewDidLoad() {

super.viewDidLoad()
//Setup
panGR = UIPanGestureRecognizer(target: self, action: #selector(pan))
panGR.delegate = self
 view.backgroundColor = UIColor.white
view.addGestureRecognizer(panGR)

}

Ok now, let’s write the most important module which will handle the dismissal of UIViewController

https://medium.com/media/3754fd89a1019af8184785c72430d08a/href

Let’s break this in to multiple parts to understand what exactly is happening inside this.

  1. Monitor the translation of view
  2. Monitor the direction of view
  3. Monitor the UIPanGestureRecognizer different states(changed and end)
  • Gesture state changed to Translate the view according to the user pan gesture.
  • Gesture state end to finish the animation.

Let’s look in to each part one by one

1. Monitor the translation of view :-

        let translation = panGR.translation(in: nil)
let progressX = (translation.x / 2 ) / view.bounds.width
let progressY = (translation.y / 2 ) / view.bounds.height

This part helps us to know how the much the view is translated relative to View both in X and Y direction.

2. Monitor the direction of view :-

        if((recognizer.direction == .up && tableView.isAtBottom) || (recognizer.direction == .down && tableView.isAtTop)){
/* When the tableview is panned down and tableview is at bottom 
OR
When the tableview is panned up and tableview is at top
*/
            if(dismissBool){
dismissBool = false
hero_dismissViewController()
self.heroModalAnimationType = tableView.isAtTop ? .uncover(direction: .down) : .uncover(direction: .up)
progressBool = true
recognizer.setTranslation(.zero, in: view)

}


}else if(recognizer.direction == .left || recognizer.direction == .right){
//When the tableview or view is panned left and right
            if(translation.x > 0){
if(dismissBool){
dismissBool = false
hero_dismissViewController()
self.heroModalAnimationType = .uncover(direction: .right)
recognizer.setTranslation(.zero, in: view)

}

}

}

This part triggers when the required Pan direction is met. dismissBool is used to track hero_dismissViewController() and to set the animation type of dismissal of viewcontroller, Called only once throughout the start of Pangesture.

3. Gesture States

3.1 Gesture states .changed to Check the pan direction the user initiated :-

                if(progressBool){
               // Pan direction up and down
let currentPos = CGPoint(x: view.center.x , y: translation.y + view.center.y)
Hero.shared.update(progressY)
Hero.shared.apply(modifiers: [.position(currentPos)], to: view)

}else{

if(translation.x > 0 ){
                   // Pan direction left and right
let currentPos = CGPoint(x: translation.x + view.center.x , y: view.center.y)
Hero.shared.update(progressX)
Hero.shared.apply(modifiers: [.position(currentPos)], to: view)

}

}

This is the part where the actual magic happens, It controls the translation of the view based on user’s pan gesture direction.

3.2 Gesture state .end to finish the animation :-

            dismissBool = true
progressBool = false
//for direction up and down
if fabs(progressY + recognizer.velocity(in: nil).y / view.bounds.height ) > 0.5 {
Hero.shared.finish()
}
//for direction left and right
else if progressX + recognizer.velocity(in: nil).x / view.bounds.width > 0.5 {
Hero.shared.finish()
}
//cancel the animation
else {
Hero.shared.cancel()
self.heroModalAnimationType = .uncover(direction: .right)

}

This part is to end animation, It checks if view is translated more the required condition which is 0.5 then it dismisses the ViewController Or Not. It also resets all variable as gesture as ended.

Final Demo

left, down, up

You can check the demo here.

Final Remarks

This is my first Medium post. If there is anything about it you don’t like or any tips you have for me, please feel free to leave some comments.

If you like what you read, please clap the article and share . Also, if you have any suggestions on what you would like to see next, please let me know in the comments below.

You can also check my other projects on my Github profile.

mukyasa (Mukesh Mandora)

Powered by WPeMatico

how-to-create-facebook-like-swipe-pulltodismiss

ABOUT THE AUTHOR