Add scroll controller to CollectionView - ios

I have a Collection View in a ViewController, with a Pan Gesture Recognizer that manages the reordering of the cells. Because of that to scroll the Collection View I want to add a "scroll controller" on the side of the Collection View, like Noteshelf app, as showing here:
My Collection view is a simple collection view with a label for every cell:

Put your note view in a scrollview and disable user scrolling by setting isScrollEnabled to false. Add a UIPanGestureRecognizer to your side bar and make yourself the target of the gesture recognizer. In your tap gesture handler function you:
Get the current translation of the pan with translation(in:)
Use this value to scroll the scrollView with setContentOffset(_:animated:)
reset the translation so you get only the difference and not the total on the next call with setTranslation(_:in:)
Edit to clairfy, UICollectionView inherits from UIScrollView so it has all of the UIScrollView properties, you just need to look under UISCrollView and not UICollectionView.

Related

UICollectionView inside UIScrollView not scrolling

I have a vertically-scrolling scroll view that has a single subview. This subview has a couple of collection views. I want them to scroll horizontally. It doesn't work because they are inside the scroll view.
The problem is that the pan/scrolling gesture is working just for the scroll view and it doesn't propagate to the collection views. I just see the scroll view scrolling.
There is the same problem with tapping on a collection cell. This event just doesn't happen.
I already tried adding the gestures manually but I am not able to scroll the collection views.
How can I propagate all the gestures ?
Thank you !
]1

Is it possible to drive a table/collection view's scrolling from gestures inside another view?

Suppose there are two UIVIew subclasses in two different regions of the screen.
The first UIView subclass is a table or collectionView
The second view is a simple UIView.
Is it possible for gestures inside the second view to be "carried over" to the first view so that for example, a swipe up gesture in the second view would make the table/collection view scroll up ?
In WWDC 2012 Session 223: Enhancing User Experience with Scroll Views, Apple engineers explained that you can take a scroll view's panGestureRecognizer and add it to a different view to make that other view control the scroll view's scrolling.
Note that UITableView and UICollectionView are subclasses of UIScrollView.
So if you can put your table view and your second view into a common superview, you can move the table view's panGestureRecognizer to the superview and it will detect touches on both subviews.
[commonSuperview addGestureRecognizer:tableView.panGestureRecognizer];
The best way to achieve that is to encapsulate your 2 views in a another view and to add the GestureRecognizer on the motherView.
This way when the gesture triggers, with the location of the touch you can figure out if it started in one view or the other and you can track the move all the way to the second view.

UITapGestureRecognizer on UITableview's background view not working

I have created a UIView and attached a UITapGestureRecognizer. I've set the view as the target of the UITapGestureRecognizer. I've then set the view as the background view of my table view. The problem is the UITapGestureRecognizer never seems to fire, even when there are no rows in my table view. Any suggestions?
Thanks.
I think no need to create a custom view with the gesture recognizer and setting it as the backgroundView for the tableView .It is better to use a Tap Gesture Recognizer with the table view it self. Like following
Here the tap gesture recognizer is connected with the table view and on tapping the table view or table view cell the event connected with the gesture recognizer getting fired(here the event is tableViewTapped: ).
Unmark the Canceled in View check box of the tap gesture recognizer(in right panel as in the screenshot) to enable single tap selection of the table view cell.

ios swipe gesture not being recognized on subview with gesture on parent view

I have a tableview that holds a number of cells with an image centered in each cell. I set a right swipe gesture on the tabelview. Each image view overrides the touches* methods. I noticed that if I right swipe outside the images, the parent view responds. If I swipe over the image views in the cells, the parent does not respond.
Does the swipe gesture get blocked by the touches* override in the child image subviews?
You may add UIswipeguesture to your image subview. So you don't need to override any touch function
Uiswipeguesture gues=uiswipeguestu
Imageview addguesture: gues;

ios move touch event between two uiscrollview

I'm building an iOS layout which consists of a UITableView and a UIScrollView. The UIScrollView is inside a table cell of the UITableView and can be scrolled both horizontally and vertically. The diagram below shows this situation. If the user begins scrolling down/up on the UIScrollView the scrolling event should trigger setContentOffset of the table view, and not setContentOffset for the scroll view while the top of the scroll view will be on the dotted line (it's constant height). Then a scrolling touch event should trigger setContentOffset for the scroll view, not for the table view.
In another case: When the user starts scrolling on the table view, it should trigger setContentOffset for the table view, until the scroll view reaches the dotted line. Then the scroll view should handle setContentOffset.
My problem is how to transfer touch events between the table view and the scroll view during one sliding action.
This sounds like one of those cases where you want something quite specific and custom. So trying to do something clever with the gesture recognizers won't be enough.
The main problem is that the ways you can control gesture recognizers such as with gestureRecognizer:shouldReceiveTouch: and gestureRecognizerShouldBegin: only affect the start of the gesture (or for new touches, not ongoing ones), but you want a single ongoing gesture to transition between controlling each view. So for this reason I think you will need to place a large transparent view over your entire screen with a pan gesture recognizer on it and in your handlePan method decide which view you want to adjust and then call setContentOffset directly on that view. You can use the translation of the pan recognizer and the existing content offset to calculate the new one. I know this isn't very elegant, but I can't think of another way to achieve the effect you want.
I'm not sure if this is going to work, but you could try doing something like this:
Option
self.scrollView.panGestureRecognizer = self.tableView.panGestureRecognizer;
Option
[self.scrollView addGestureRecognizer:self.tableView.panGestureRecognizer];
Option
[self.tableView.panGestureRecognizer requireGestureRecognizerToFail:self.scrollView.panGestureRecognizer];

Resources