Pass UITableView scrolling gesture to superview UITableView - ios

I have a SuperUITableView as the main superview; inside that superview, I have another UITableView called SubUITableView.
I want to pass the scrolling gesture from the subview (SubUITableView) to the superview (SuperUITableView)
If the question is not clear, kindly ask me anything in the comments instead of downvoting.

UITableViews will confirms to scrollViewDidScroll delegate.
Inside the delegate of inner tableView, you just programmically scroll super tableView.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
UIScrollView *parentScrollView = (scrollView == self.tableView1) ? self.tableView2 : self.tableView1;
[otherScrollView setContentOffset:[scrollView contentOffset] animated:NO];
}

Related

Right way to set Delegate of ScrollView in UITableViewCell

I have a Custom UITableView Cell as xib.
I have taken an Scroll View inside it.
I Know how to set the delegate but I have confusion.Their is two of doing this.
I don't knw which is best and How to decide which way I have to choose.
1 Way : To set Delegate To Files Owner
2 Way : setting Delegate to UITableViewCell
In a tableviewcell the delegate of scrollview will always be set to UITableViewCell
That means your second step will work .
Let me know if you find any difficulty.
The choice depends on what the delegate function for the inner scroll view must do. It is simpler to point the delegate to the custom cell and handle the inner scrolling events there.
But if handling that inner scrolling requires a lot of data and logic from the view controller, then point the delegate to the vc. To make that work, we'll need to know that the scroll event was from an inner scroll view, not the table, and we probably need the row where the scrolling happened, so:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// if the table view's delegate points here (likely), then to distinguish...
if (scrollView != self.tableView) {
// one of the scroll views in the table, but which one?
NSIndexPath = [self indexPathContainingView:scrollView];
// here we know that horizontal scrolling happened on indexPath.row
}
}
// return the indexPath of the tableView cell containing a view
- (NSIndexPath *)indexPathContainingView:(UIView *)view {
while(view && ![view isKindOfClass:[UITableViewCell self]])
view = view.superview;
return [self.tableView indexPathForCell:(UITableViewCell *)view];
}

UITableView inside UIScrollView, scroll not working

I'm trying to implement the following hierarchy
-- UISCrollView
-- ContainerView
-- UITableView
Initially the UITableView scroll is disabled. When the UICollectionView reaches a particular contentOffset of the UIScrollView, I implement this :
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.y >= 140)
{
[scrollView setContentOffset:CGPointMake(0, 140) animated:NO];
}
}
All this works as intended, but now the UIScrollView is eating the scroll of the UITableView. The scroll of UITableView is erratic now and doesn't scroll properly. The scroll of UITableView happens only after 7-8 attempts and then it stops again.
What might be the reason of this behaviour? I tried setting myScrollView.scrollEnabled = NO inside scrollViewDidScroll and no luck.
You can delay the main scroll view's touch response, then the sub tableview (scrollview) can be scrolled correctly:
myScrollView.panGestureRecognizer.delaysTouchesBegan = YES;

Forward vertical scrolls from UIScrollView to a sibling UITableView

I have a view controller with this hierarchy:
View Controller:
UIScrollView (scrollable horizontally)
UITableView (scrollable vertically)
I want to forward the vertical scrolls from my UIScrollView to the sibling UITableView, so that when the user scrolls up on the UIScrollView, the UITableView will scroll up instead. What would be the best way to do it?
I have tried these:
Detecting the vertical scroll in scrollViewDidScroll, it doesn't get called because the contentOffset of the scroll view does not change.
Subclassing the UIScrollView and overriding touchesMoved, I can't forward the touches to the table view because I don't have a reference to it in this class.
If the tableview is contained within the scroll view I believe you can set up the scroll view's gesture recognizers to respond only if the table view's gesture recognizers fail. I haven't had a chance to try this, but you should be able to set up a dependency between the gestures for each of the views.
UITableView* tableView = ...;
UIScrollView* scrollView = ...;
for (UIGestureRecognizer* r in scrollView.gestureRecognizers)
{
for (UIGestureRecognizer* tableRecognizer in tableView.gestureRecognizers)
{
[r requireGestureRecognizerToFail:tableRecognizer];
}
}
This will make your scroll simultaneously with UITableView and UIScrollView and apply #Stephen Johnson's block
- (BOOL)gestureRecognizer:(UIPanGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UISwipeGestureRecognizer *)otherGestureRecognizer
{
return YES;
}

Lock handling of scroll event in UIScrollView subview (inside a UIScrollView superview)

I have a complex view hierarchy:
UIScrollView
- UITableView
- UICollectionView
The UICollectionView and UIScrollView both scroll horizontally. Now, I want to capture the swipe gesture exclusively on UICollectionView so that the superview (UIScrollView) ignores it.
User can swipe left to get to the end of UICollectionView. When at the end, the swipe gesture on UICollectionView is ignored and captured by the superview (UIScrollView) and the interface changes. How can I prevent that from happening?
Note: I'm using the default swipe functionality provided by both UIScrollView and UICollectionView i.e. I'm not adding any custom swipe gesture handling.
In your ViewController.m
-(void)viewDidLoad
{
scrollView.delegate = self;
collectionView.delegate = self;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
if (the scrollView didn't reached the end){
then collectionView can't scroll
}else{
collectionView can scroll
}
}
You can check to see if the scrollView reached the end with the contentOffset property
Don't forget to declare that you are using the delegate of scrollView
Hope that helps

scrollViewDidScroll getting called for uiscrollview inside tablecell while scrolling tableview up and down

I have a custom uiscrollview inside each table cell. It works great except one condition. When you scroll the scrollview and it do autoscroll/decelerate. At same time if you scroll table view quickly up and down it also moves the scrollview in the table cell which will result in scrollViewDidScroll getting called for scrollview and messed up with my logic.
You need to check which UIScrollView is actually scrolling:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView == yourScrollView)
{
// Do something
}
else
{
// Do something amazing
}
}

Resources