Swiping left to go back in swift - ios

This is how my storyboard is set out:
And I want to be able to swipe left and right within view controllers. However I am not using segues, I am changing view controllers programatically. I also need to be able to not swipe back to the sign up and log in pages from the home page.
How do I do this?
I'm not sure if it is necessary however the code I am using the change view controllers is:
let codeViewController = storyboard?.instantiateViewController(identifier: Constants.Storyboard.codeViewController) as? CodeViewController
view.window?.rootViewController = codeViewController
view.window?.makeKeyAndVisible()
EDIT
I understand I should not use the code above and should use Segues, however I have to change the view programatically from the sign up and login pages. And I cannot have 2 Navigation Controllers so what do I do?

Add a UISwipeGesture and add the animation for it.
let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
swipeDown.direction = .down
self.view.addGestureRecognizer(swipeDown)
If you want to paging functionality, you can utilize a UIPageViewController. Here is a basic example regarding UIPageViewController.

Related

How do I achieve the full screen swipe/pan back feature for navigation controller?

I'm implementing a feature in my app that allows user to swipe/pan back to previous controller from anywhere on the screen. But the default swipe/pan back feature provided in navigation controller only works for the screen edge. How can I have it work from anywhere on the screen or how do I achieve something like 'full screen swipe/pan' back feature?
You can add swipe gesture to view of viewController like this
let recognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(self.swipeToGoBack))
recognizer.direction = .right
self.view.addGestureRecognizer(recognizer)
#objc func swipeToGoBack(_ recognizer:UISwipeGestureRecognizer) {
// add code to dismiss this controller
}

How to swipe a View controller over the top of another view controller?

I have a table view controller and another view controller. Now my requirement is that i need to swipe the table view controller half over the another view controller when i swipe the view controller. The image i can show is like this:
Is it possible to achieve this by using the Swipegesture . If possible how can i do this in Swift3?
While there are libraries out there to do this for you, if you are going to do this yourself, the basic idea is that you can create a "swipe from edge" gesture recognizer to initiate a custom presentation of a view controller (that has your menu on it). It consists of:
a custom transitioning delegate (conforming to UIViewControllerTransitioningDelegate protocol) that specifies the animation controller and interaction controller (both described below) to be used during the presentation of the next scene;
an animation controller (conforming to UIViewControllerAnimatedTransitioning) that dictates the animation of the scene from the right edge;
an interaction controller (a UIPercentDrivenInteractiveTransition) that you can use to optionally drive the transition via a gesture;
a presentation controller (a UIPresentationController subclass) that dictates whether the presented view will be removed or not (in this case you do not want it removed), as well as any other chrome to be animated alongside the animated presentation of the new scene (e.g. you often dim or blur the presenting view); and
gesture recognizers to drive the interaction controller.
For more information, see WWDC videos Custom Transitions Using View Controllers and A Look Inside Presentation Controllers.
See https://github.com/robertmryan/SwiftCustomTransitions/tree/rightside for a Swift 3 example that renders the following UX:
This is all admittedly complicated enough that you may well want to consider third party libraries for presenting slide-in menus. But if you wanted to "roll your own", these are the basic pieces involved.
What I would do is first create and hide a UIView that lets you select those UIViewController on the right side of the screen and animate it to show when the user swipes.
Then you implement this method that returns UIViewControllerAnimatedTransitioning, a class that you want to implement for custom transitioning.
- (id<UIViewControllerAnimatedTransitioning>)
navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController*)fromVC
toViewController:(UIViewController*)toVC
This could be a good tutorial for custom transitioning.
https://www.raywenderlich.com/110536/custom-uiviewcontroller-transitions
I would look into SWRevealViewController. It gives you the behavior you are looking for in just a few lines of code. You can present an SWRevealViewController that will hold your top and bottom UIViewController and present that as your scene.
// Your Front View Controller
let frontStoryboard = UIStoryboard(name: "FrontStoryboard", bundle: .main)
let frontVC = frontStoryboard.instantiateInitialViewController()
// Your Rear View Controller
let rearStoryboard = UIStoryboard(name: "RearStoryboard", bundle: .main)
let rearVC = rearStoryboard.instantiateInitialViewController()
// Create Reveal View Controller From Both
if let revealVC = SWRevealViewController(rearViewController: rearVC, frontViewController: frontVC) {
present(revealVC, animated: true) {
}
}
Using SWRevealViewController you can set the UIViewController that you are trying to do the sliding in as the frontViewController and you can present the rearViewController simply using a single line of code.
// When you import SWRevealViewController every UIViewController
// has a method revealViewController() that returns the revealViewController
// that you can tell to toggle it's reveal state using the below
self.revealViewController().revealToggle(animated: true)
EDIT
I believe you are trying to build a sliding navigation menu. Here is a link to a Ray Wenderlich tutorial where he is creating exactly the navigation you are looking for.

Swipe left view controller to dynamic view

I'm currently building an app that needs to be able to swipe left to the next view controller and then show random quotes dynamically from a dictionary.
I've added the current gesture recognizer from the initial view controller however I don't want the standard push segue functionality.
How it should function:
From swiping left you are swiped to the next view controller. From this view controller I could use a scroll view. Could I generate dynamic sub views based on the count of the dictionary or is there something else.
Current gesture recognizer:
let swipeLeft = UISwipeGestureRecognizer(target: self, action: "goSwipe:")
swipeLeft.direction = .Left
self.view.addGestureRecognizer(swipeLeft)
I would like to be able to swipe left on the inital view controller to the next (there's only 2 VC's in the app).
Thanks
you can use horizontal scrollview by setting down its contentSize method x position then generate dynamic sub views based on the count of the dictionary.
Check this out here.

Sending touch events from view to controller

I have custom UIView that is being displayed in controller's view. I need to display a view controller modally when something in that custom view is tapped. What is the most efficient way to notify the view controller (if it exists) about the touch from the custom view's touchesBegan()?
You can use UITapGestureRecognizer. Declare UITapGestureRecognizer like this and add gesture recognizer to your custom view
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapOnCustomView(_:)))
customView.addGestureRecognizer(tapGesture)
Now receive the tap
func tapOnCustomView(tap:UITapGestureRecognizer){
// present your targeted view controller modally
}
NB: UITapGestureRecognizer allocation syntax vary with Swift version. This one is for Swift 2.2

Sliding tableViews (much like calendar)

What do you recommend would be the best way to create the sliding tableView effect seen in calendar, where you can "swipe" between tableViews?
Currently, the way I have it working is detecting swipe gestures, and then reloading 1 tableView based on the swipe direction. While this works, it doesn't look all that great. I really want the effect where as they drag right/left the next tableView is dragged in.
var rightRecognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "handleSwipeFrom:")
rightRecognizer.direction = .Right
var leftRecognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "handleSwipeFrom:")
leftRecognizer.direction = .Left
self.view.addGestureRecognizer(rightRecognizer)
self.view.addGestureRecognizer(leftRecognizer)
func handleSwipeFrom(gesture: UIGestureRecognizer) {
println(previousDays.count)
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Right:
//call function to get new data
self.tableView.reloadData()
case UISwipeGestureRecognizerDirection.Left:
//call function to get new data
self.tableView.reloadData()
}
default:
break
}
}
}
To achieve this , instead of using TableView, you can use UICollectionView.
Please refer following reference links to implement UICollectionView;
1) Link 1
2) Link 2
3) Link 3
Hope this will help.
What I have done in the past is create a UIScrollView that will scroll horizontally and add each TableView inside of that side by side. You can then set the pagingEnabled property to yes so that when you scroll horizontally the scrollView will snap to show only one tableView at a time.
It is really quite fluid and simple to implement.
You should use collection view and in each collection view cell contains tableview. you can also use scrollview but in scrollview you have to manage content-size and other thing and scrollview don't deallocate memory after scrolling while in collection view only load current cell memory.

Resources