UIScrollView Vertical Scrolling when touching inside a subview - ios

I have implemented a scrollview, which holds multiple copies of a custom view. The custom views contain a UIPanGestureRecoginixer that I use to swipe them to the left (to delete them). When I attempt to scroll vertically, scrolling doesn't occur if I'm touching inside of the CustomView. However if I set up the subViews and the scrollviews content size so that it scrolls horizontally this issue doesn't occur. How can I force the scrollview to scroll horizontally even if I'm touching inside of a subview?

Try to set the UIPanGestureRecoginzer's delegate and implement
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
It requires you to return a BOOL value. When you return YES, the otherGestureRecognizer will be recognized simultaneous.

Related

How can I catch the gestures over UICollectionView?

I have a UICollectionView and a custom UICollectionViewCell
I want to be able to catch the UICollectionView gestures as a UIGestureRecognizerDelegate, actually I want to handle some gestures collisions by using this delegate's method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
How can I catch the UICollectionView's UIGestureRecognizerDelegate?
UICollectionView does listen for taps but not by using a UIGestureRecognizer.
But you could add your own UIGestureRecognizer for the type your interested in (like UITapGestureRecognizer for instance) to the UICollectionView, set the delegate on it and in gestureRecognizerShouldBegin: return YES or NO depending on whether you want the UICollectionView to do it's thing or not, i.e. returning NO would cancel your gesture and allow the collection view to handle the touches.
Or set delayTouchesBegan to YES if you just want your gestures to take priority over the collection view touch handling.
More info is here Collection View Programming Guide

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

UIbutton method not getting called when added to view, scrollview & tablecell

I am doing following steps and having problem with UIButton not getting called.
1) I create UIbutton objects array using for loop. called addTarget for all buttons and set tags also.
2) Used above array, placed all buttons to look like table columns in a view.
3) Add the above view in scroll view.
set scrollview content size = view size
4) Now add above scrollview to cell.contentView as subview.
The issues I'm facing are :-
1) The scroll view not getting scrolled horizontally.
2) The button is not calling its methods
Your Using Tableview and Scrolle view , Both are no need , If using Tableview why scroll view
If your using only scroll view follow below url
Horizontal scroll with buttons in ios?
If you want scroll view to scroll in table view cell you have to provide delegates to both table view and scroll view and make them implement:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES; }
Do not forget that content size of the scroll view must be bigger than it's frame size in order to scroll. Also, try setting contentView.userInteractionEnabled = NO; for button to become click-able.

UIScrollview scrolls to top when paging is Enabled

I have the UIScrollView with pagingEnabled set to YES, and programmatically scroll its content offset to a particular point where the user taps using the following code:
[self.scrollView setContentOffset: CGPointMake(x, y) animated: NO];
It scrolls successfully to this point (x,y), but now if I do a single tap ,its content scrolls up to top.
When paging is disabled , it does not scroll to top but I need paging to be enabled.
Does any one know how can it be fixed?
Try to use this function:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
check the class of the otherGestureRecognizer if it's a tap return NO, else return YES.

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