Sliding tableViews (much like calendar) - ios

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.

Related

Gesture recognizer callback over multiple views

I have a custom UIView that consists of 9 equally-sized subviews. It essentially looks like a tic-tac-toe board.
I've added a UIPanGestureRecognizer to this custom view. Each time the user pans over one of the subviews, I want to take an action. For example, the user could pan over the first 3 (of the 9) subviews in one gesture, and in this case I'd want to take 3 actions.
I could try to do some fancy math and figure out the frame of each subview, then figure out when the gesture crosses from one subview to another. However, I feel like there should be a more elegant way to get a callback when a gesture touches a new UIView. Does a functionality like this exist?
I was able to find a more elegant way by using hitTest, which returns the lowest subview with user interaction enabled. I defined the callback for the pan gesture recognizer as such:
var panSelected = Set<UILabel>()
#objc func handlePan(recognizer: UIPanGestureRecognizer) {
let view = recognizer.view
let loc = recognizer.location(in: view)
if let gridLabel = view?.hitTest(loc, with: nil) as? UILabel {
if !panSelected.contains(gridLabel) {
// my code here
}
}
try to use stack view to group views have same gesture, & using
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(stackViewTapped))
myStackView.addGestureRecognizer(tapGesture)
if there's different method needs to be implemented, just add another stack view.

Use right to left UIScreenEdgePanGestureRecognizer on UITableView that has section indexes

I want to add a UIScreenEdgePanGestureRecognizer to a UITableView so that I can edge swipe from the right side to go to the next screen in my controller hierarchy.
This works fine except when the table view has section indexes shown on the side. In that case, the section index area handles the touches, so I can't swipe from the edge. I would like to be able to support both the edge pan and the section index tap and vertical pan functionality.
I tried adding a view on top of the UITableView to handle the swipe, but then that handles all touches and the table view no longer gets anything.
Okay, so I tried a bunch of stuff and came up with a non-ideal but working solution.
The first part of the problem is that the section index is in its own view as a subview of UITableView. So it captures any touches or gesture recognizers that you might add to the UITableView.
The second problem is that the section index view is not part of the public API.
So my solution is to add a UIScreenEdgePanGestureRecognizer to the section index view.
The view is an instance of the private class UITableViewIndex. To get it I created a UITableView extension:
extension UITableView {
var sectionIndexView: UIView? {
for view in self.subviews {
if view.className() == "UITableViewIndex" {
return view
}
}
return nil
}
}
NOTE the above code is fragile and not future proof, because if Apple changes the class name or its location in the view hierarchy it will no longer work. In my case the edge swipe is a convenience feature, and there is a "Next" button at the top of the screen. In addition, most of my view controllers don't have a table index, so if it stopped working it would only be on a few screens. So if this solution doesn't work in the future then it's not a huge deal.
I also wrote an convenience function to get the edge gesture recognizer from any view:
extension UIView {
var screenEdgePanGestureRecognizer: UIScreenEdgePanGestureRecognizer? {
guard let gestures = self.gestureRecognizers else {
return nil
}
for gesture in gestures {
if let edgePan = gesture as? UIScreenEdgePanGestureRecognizer {
return edgePan
}
}
return nil
}
}
I add a UIScreenEdgePanGestureRecognizer to the UITableView in the viewDidLoad method of my view controller.
Finally, in viewDidAppear of my view controller I check to see if the gesture recognizer has been added to the section index view. If it hasn't, then I add it there. You need to do this at some point after the section index has been added to the table view - i.e. not in viewDidLoad. And you need to make sure you're not adding the gesture recognizer multiple times.

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.

Swift UITableView Swipe During Scroll Animation

I've looked around and tried everything I can think of sort of subclassing TableView, but I think I'm missing something. I have a TableView with entries that I can swipe left and right on, and everything works fine. However, if:
1) I start (vertically) scrolling a little bit, and then swipe, the TableView's superclass, ScrollView, seems to block the swipe from my TableViewCell.
2) I stop scrolling, but the animation hasn't fully stopped, the swipe is still blocked from the TableViewCell.
How can I allow my swipe to get passed through to my TableViewCell, regardless of the vertical scroll?
Swift 2.2, Xcode 7.3
I fixed this basically by doing what was suggested in this thread: Tell ScrollView to Scroll after other pan gesture
Below is code that should enable someone else that comes across this thread to scroll in a table view, and be able to swipe, without having to deal with the table view's pan gesture recognizer blocking the swipe due to the mere hint of vertical motion.
Hope it helps someone.
So (inside a UITableViewController -- non--essential code emitted):
var lsgr : UISwipeGestureRecognizer!
override func viewDidLoad(){
super.viewDidLoad()
self.lsgr = UISwipeGestureRecognizer(target: self, action: "didSwipeLeft:")
self.lsgr.direction = .Left
self.lsgr.cancelsTouchesInView = false
self.lsgr.delegate = self
}
func didSwipeLeft(leftSwipe: UISwipeGestureRecognizer){
var ip = self.tableView.indexPathForRowAtPoint(leftSwipe.locationOfTouch(0,inView: self.tableView))
print("swipe left - "+String(ip!.row))
}

How to create a dragging animation for a UIView?

I have a hidden UIView at the bottom of my UIViewController.
I would like to create an interactive animation that would show progressively my UIView, like if I was dragging the UIView from the bottom and it would follow my finger (location, speed, etc.). If I go over half way the final location of my UIView and release my finger, it would continue, if I don't reach this half way, it would go back hidden. Basically, the same behaviour like the control center.
The problem is I don't know where to start. Can someone point me to the right direction?
You should use UIGestureRecognizer,for example UIScreenEdgePanGestureRecognizer
keep a property
var gestureReconginzer:UIScreenEdgePanGestureRecognizer?
Then in viewDidLoad,init the gestureReconginzer
gestureReconginzer = UIScreenEdgePanGestureRecognizer(target: self, action: "catch:")
gestureReconginzer?.edges = UIRectEdge.Bottom
self.view.addGestureRecognizer(gestureReconginzer!)
When the gesture is Reconginzed
func catchGestrue(gesture:UIScreenEdgePanGestureRecognizer){
switch(gesture.state){
case .Began:
//Set your view hidden = false
case .Changed:
//Change your view center
case .Ended:
//Decide if your view reach half way.
//Use UIView.animateWithDuration to let your view return or go to right place
default:
}
}
BTY: I do not think pull a view from bottom is a good idea.
I have a simple objective C project in here,if you know objective C,you may refer the gesture part.

Resources