Swift Default Swipe Back - Animation - ios

i am trying to make a swipe back animation in my App. It is working completely fine, but there are two problems I have.
The First one is, that the back swipe is working on the complete page and not only on the left side of my display.
And the second questions is, how can I have this normal back swipe animation. In my App it directly pops up.
At the moment I am only using ViewController and to switch between them I use a button with perform segue. But to get back to the pervious page I would like to use this default swipe back animation Apple is having.

Well there are multiple things happening.
the best way to handle this is to embed your first view controller inside UINavigationViewController. You can then push your second view controller and so you will get that Apple back-animation for free.
if you don't want to use NavigationController, you can at least replace that swipe recognizer with UIScreenEdgePanGestureRecognizer, so it will trigger only near the edges of screen. But it will still just pop instantly. You can't easily replicate that back animation... you'd need to use custom transition with
UIViewControllerTransitioningDelegate. And that's not easy.
My advice: learn more about UINavigationController and use it.

Related

Using Pan gesture recognizer to bring down a view controller

I want to be able to swipe down on my main view controller and have my second view controller be brought down from above. I want this transition to be interactive in the sense that you can go back and forth between VCs without having to complete the whole transition (following the finger of the user like a scrollview with paging does).
This is effect can be seen when sliding between View Controllers on the new snapchat update. When you first open the app you are on the camera screen. If you swipe in any direction (up, down, left or right) the corresponding view comes in and covers the camera screen. When you swipe between views they are all essentially covering the main view (not moving it of to the side like in many tutorials i find online which use scrollview to achieve this effect).
Essentially it would look like a vertical page view controller, but instead of moving the current view controller off the screen as the new one comes in, the new one would cover the current one until it is completely on top of it.
From what I understand, a pan gesture recognizer could do the trick.
Does anyone know how I can go about doing this. I have been searching forever and can not seem to figure it out.
I want to be able to swipe down on my main view controller and have my second view controller be brought down from above. I want this transition to be interactive
Okay, so you are describing a custom interactive transition animation. The exact details for how you implement this depend on whether you want this to be a push transition or a present (modal) transition. I assume it is to be a presentation (modal) transition.
So you will start by setting the presented view controller's transitioningDelegate. Everything takes off from there. The pan gesture recognizer will call present,
and the transitioning delegate's delegate methods will be called:
animationController(forPresented:presenting:source:)
interactionControllerForPresentation(using:)
The interaction controller that you return from the second method is responsible to responding to each change in the pan gesture by updating the "frame" of the animation (as well as the transition coordinator).
If this is for iOS 10 only, this is very easy because you can use the UIViewPropertyAnimator. It has the remarkable ability to "hurry" to the end or start of the animation when the gesture ends and you decide to complete or cancel the animation. Otherwise, you're probably best off using a UIPercentDrivenInteractiveTransition object to help you.

How do I create two custom segues, one for each direction?

I was able to look up a way to transition between two ViewControllers using a UIScrollView that I can use as a transition between two VCs.
I have an issue however, I have a TableView with cells that trigger a different kind of segue when tapped. (The Segue is some labels move to the new VC and other stuff appears)
I then want to be able to swipe back and forth all while the first animation is controlled by how much I swiped between. For example. if the user swipes just a small bit I want the animations to reverse back, so that if he changes his mind, he can still stay on his current VC. But if he wants to go back to the old VC, the labels animate back in place.
Basically like Snapchat but only one to go back, and the labels revert back. The other way is by tapping.
Here is a video mockup of the animation, please excuse its crudeness
You need to read up on transition controllers. What you want to do is to create a custom transition controller, specifically a UIPercentDrivenInteractiveTransition. Take a look at this tutorial on the subject:
custom UIViewController Transitions

Transition of a view controller embedded in a navigation controller

I have a problem with a transition. I am modifying this project https://github.com/xxxAIRINxxx/MusicPlayerTransition in order to have the transition from the right instead of the bottom. This part is fine. My transition comes from the right.
My problem is that I'm embedding the presented modal view controller into a Navigation Controller. I do this so I can use the "pushViewController" function when I click on a cell of my modal view controller.
My hierarchy is as follow. I have a dashboard. From the right, I drag a TableViewController X. If I drag back in the opposite direction, I come back to my dashboard. Fine. Now, If I tap a cell, it pushes a new ViewController Y.
Now, if I am in my new ViewController Y and that I drag back, I come back directly on my dashboard instead of going back to my TableViewController X, as a natural navigation controller should behave.
I did a small project on github that does only that, so you can easily see my entire code.
https://github.com/magohamote/NavigationControllerTransition.git
I understand why it behaves like this, but I would like to know how I can override the transition set on the navigation controller in order to have the normal behaviour of my navigation controller once it is presented.
Another problem I have not been able to solve. On the project I used as a starter (the one from xxxAIRINxxx) the transition is perfectly smooth. On mine, the first time I trigger the transition, it blinks and get stuck. Once I did it once, it is smooth the next time I drag my view. But the first time is always awful. I don't know why either :(
Thank you a lot for your help!

iOS : Swipe Gesture Push in one ViewController?

I want to add two swipe gesture. I did the code, and it's running, my text in the viewcontroller change when i touch the screen.
BUT, i want to add a "effect" when the user wants to slide at the next (or the previous) news, because currently, i'm just changing the text.
i tried to add action in the storyboard, to add push on the swipe with the view.
It's running, but there are two problems :
The push, seems to have a problem, because, when i want to slide on the previous news, the push effect comes right to left. (and not left - right)
I can't only change the contents without touch the navigation bar ? (because when i slide, the navigation bar loose him title, etc..)
If I understood you correctly you might want to use a UIScrollView. This will give you a nice animation.
You need a UIScrollView and add two of your ViewControllers as subviews.
Here are links to some ScollView tutorials:
Are there any good UIScrollView Tutorials on the net?
Pushing on the navigation controller stack is to the right if you want to go back to opposite direction instead of pushing, you should pop like so: [self.navigationController popViewControllerAnimated:YES]; You could paste some code in your question :)

Created custom UINavigationController - using the iOS 7 swiping gesture doesn't work properly

So I had created a very simple custom NavigationController a couple years ago, of course it subclasses UINavigationController and the only method it overrides is
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
in it I show a UIAlertView asking the user to make sure they want to go back. When they select yes it goes back using
[self popViewControllerAnimated:YES];
Everything is working right except for with iOS 7 when using the new swiping from left to right gesture to go back to the previous screen.
It correctly goes back to the previous screen however the top navigation bar doesn’t change.
For example if I’m on my main screen and I click on Row1. The view will change and the nav bar will display Row1 as the title and a ‘< Back’ button on the top left.
Clicking on the ‘< Back’ button works 100% as expected.
If I swipe left to right to go back, the view will go back to my main screen however the nav bar will still be showing the ‘< Back’ button and ‘Row1’ as the title.
I tried just using UINavigationController instead of my custom class which subclasses it and the swiping features work correctly so I know that’s the problem. I must be missing a call or something in my custom class
Any ideas?
Thanks!
EDIT:
I just edited my CustomNavigation code a bit and it now seems to work. When using the top left Back button for some reason shouldPopItem was getting called twice, so I had some code to make it work correctly with iOS 6 and lower.
When using the swipe gesture I noticed that it was only being called once and the first time I was popping the controller but returning NO (which was why the nav bar wasn't changing). I now put a check for iOS 7 and return YES right away which fixed the problem and now works properly.
navigationBar:shouldPopItem: is a delegate method on UINavigationBar, which UINavigationController implements. So all you're doing is stopping the navigation bar from popping a UINavigationItem. That's why when you override this that it prevents the navigation bar from removing the item associated with the UIViewController being popped. It shouldn't actually ever prevent the navigation controller from popping a view controller, and it was probably just a side effect of the old implementation that made it work right. Either they changed something, or there's just a difference with how it works when the gesture is used, that makes this hack no longer work right. Anyway, generally you're not supposed to be able to prevent the back button from going back. If you want to present a screen that requires some kind of confirmation to go back, or a task to be completed or cancelled first, you should present a modal view controller instead. Otherwise, you could replace the default back button with your own and hide the normal back button. Then your custom button (which would look different unless you went to a lot of work) could call a custom method to prompt first.
Of course you'll want to disable the pan gesture if you stick with using a UINavigationController. There's a property on UINavigationController to get the gesture recognizer:
#property(nonatomic, readonly) UIGestureRecognizer *interactivePopGestureRecognizer NS_AVAILABLE_IOS(7_0);
You can disable it so that it will no longer make you go back.

Resources