Swipe left view controller to dynamic view - ios

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.

Related

Swiping left to go back in swift

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.

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

Single tap gesture to multiple View using storyboard

How to have single tap gesture to multiple Views using storyboard.
I drag 3 views and one tap gesture into UIView class.
Contacted three view to tap gesture and added action class handleGesture Method on tap on any one the tree view it should trigger the method action.
using story board.
But i want to do it with single tap gesture is it possible or not.
try with this, define a variable UITapGestureRecognizer with your method, after that, in a method or wherever you want, you can add this gesture to your multiple views
var recognizerMovements: UITapGestureRecognizer {
get { UITapGestureRecognizer(target: self, action: #selector(self.myActionMethod)) }
}
self.myFirstView.addGestureRecognizer(myActionMethod)
self.mySecondView.addGestureRecognizer(myActionMethod)

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.

Recognize swipe gesture in view not in subview

I have added a subview to a View Controller's view. This subview is the view of QLPreviewController.
What I am trying to achieve is to recognize swipe gestures on the subview in the parent view, i.e. the View Controller's view. In the end, I want to be able to swipe left /right on the view to load the next document for preview.
I'm aware of hit testing and understand that by just attaching a gesture recognizer to the parent view, those will not be recognized, since the subview will be the "hit-test" view.
Now what is the best (or easiest) way to recognize those gestures?
Note: I didn't manage to attach the gesture recognizers to the subview, this doesn't seem to work.
* UPDATE *
To make this more clear - this is the code from my ViewController. vContent is just a view in my ViewController, where I add the view of the QLPreviewController:
let pvVc = QLPreviewController()
pvVc.dataSource = self
vContent.addSubview(pvVc.view)
I tried adding the swipe recognizers both to the vContent and the pvVc.view. In both cases no event was fired.
let sgrLeft: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action:Selector("handleSwipe:"))
sgrLeft.direction = UISwipeGestureRecognizerDirection.Left
sgrLeft.delegate = self
On some other view the code works fine.
Any hint is appreciated!
Thx
Eau
Well, the responder chain, the unknown animal … ;-)
You can subclass the superview and override -hitTest:forEvent:.
You rarely need to call this method yourself, but you might override it to hide touch events from subviews.
Gesture Recognizers Get the First Opportunity to Recognize a Touch, so even the subview is hitTest view. the gestureRecognizer attached on superView can recognizer touch event.

Resources