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

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

Related

Pass UITableView scrolling gesture to superview UITableView

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];
}

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;
}

Resize the subview when scrollview scroll in UIPangesture

I have Create UIScrollview Programatically.I have added 5 View and each view have Imageview with image. I am doing zoom in/zoom out image using UIPanGesture i can zoom in and zoom out image using PanGesture but when i scroll the scrollview then UIImageview not set its actual frame. i want to resize subview of scrollview when scrollview scroll.
Thanks in Advance
Set your view controller a delegate of your scrollview:
scrollview.delegate = self;
Then, implement this delegate method in your viewcontroller:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
// resize subview of scrollview here
}
You need to set some object as the scrollview's delegate and then implement this method:
-(void)scrollViewDidScroll:(UIScrollView *)inScrollView
Then you can do whatever you would like with the subviews whenever the scrollview scrolls (this includes automated scrolling, not just when the user scrolls).

UITableView in UIView in UIScrollView. When scrolling tableview don't scroll scrollview too

I have a UITableView inside a UIView inside a UIScrollView.
Problem: when i scroll/drag the tableview, the scrollview scrolls too.
What i need is for the tableview not to pass the scroll to the scrollview. Also i need to be able to use the scrollview when i scroll it directly.
How can i do this?
Cheers
I fixed it using "hitTest" on the tableview.
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
If the event originated within the tableview, i disable the parent's scrollview scroll.
And, when the tableview's scroll has ended (scrollViewDidEndDragging) i re-enable the parent's scrollview scroll.
This seams to work fine.
Set a delegate to your tableview's scrollview (i.e your view controller)
tableView.scrollView.delegate = self;
then use those two calls
– scrollViewDidScroll:
– scrollViewDidEndDragging:willDecelerate:
to disable and re-enable your outside scrollview's scrollEnabled property

Resources