I have a multidirectional UIScrollView within a horizontal UIScrollView within a vertical UIScrollView. When I reach the horizontal content limit of the innermost scrollView, the containing horizontal scrollview begins scrolling just as I want.
But when I reach the vertical content limit of the innermost scrollView, I do not get any action on the top level vertical scrollView.
If all vertical content is visible at innermost level (so no vertical scrolling is possible), then the top level vertical scrollview takes over fine. Any clues on how to fix this?
I get this same behavior with Apple "PhotoScroller" sample code if I embed their photo paging scrollView in a vertical scrollView. If the photo is not zoomed, then both the horizontal and vertical scrollviews will work. If the photo is zoomed but panning stops because it has hit the horizontal or vertical limit, then only the horizontal scrollview works.
This is using an older version of the sample code that implements the photo paging as a UIScrollView rather than UIPageViewController. With current UIPageViewController version, paging is not possible when the photo is zoomed.
To answer my own question, I used gestureRecognizerShouldBegin: delegate to determine whether the innermost scrollView was at a boundary when the pan gesture began. If so, I responded to shouldRecognizeSimultaneouslyWithGestureRecognizer:
delegate to enable the UIPanGestureRecognizer on both the inner scrollView and the vertical scrollView. This allowed a single gesture to scroll both simultaneously. Then, I over-rode setContentOffset on both scrollViews to suppress the scrolling on the wrong one.
I think a better answer is probably to replace the UIPanGestureRecognizer on the inner UIScrollView with a custom gesture recognizer that cancels itself when started at a boundary and dragging past the boundary. But, I could not get that to work.
Still not clear to me why the horizontal version of this just worked automatically. Perhaps there is some special case handling in the UIScrollViewPanGestureRecognizer to handle horizontal case, or interaction with immediate superView.
Related
iOS 8, Swift.
I'm trying to have a vertically scrolling view over a horizontally scrolling view. They're both UIScrollViews. The vertical scroll view is there to allow swiping up a view from the bottom. There is a spacer view on top using auto layout that is 1 pixel wide but the screens height.
This works fine until the underlying view is itself a scrollview to support horizontal scrolling.
I somehow need to pass the left/right pan gesture to the subview which is a UIScrollView.
Currently, the top level vertical scroll view is capturing all the gestures and not letting the underlying horizontal scroll view see the events.
I've tried various combinations of hitTest, gestureRecognizer delegate methods, scrollview subclassing but having come up with a nice clean solution.
I can use hitTest to pass events to the underlying horizontal scroll view when tapping the empty space at the top of the vertical scroll view, but then the vertical scroll view never processes a pan or swipe up to reveal the content that should appear on a swipe up.
Ideally, I'd like the top vertical scroll view to only handle pan up/down, and pass left/right pan to subview when the vertical scroll view is at the top.
Here's a brief method that may help you out:
Obj-C:
create scroll view that is invisible, and then pass the offset of the invisible scroll view or the touch events to the scroll views below depending on the offset of the touch events on the top invisible scroll view that covers the two underlying scroll views:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
[_verticalScrollView setContentOffset:CGPointMake(0, _insivibleScrollView.contentOffset.y)];
[_horizontalScrollView setContentOffset:CGPointMake(_insivibleScrollView.contentOffset.x, 0)];
}
You create an invisible scroll view, and by invisible, I mean you may have to keep the alpha value at like 0.01. You don't allow user interaction for the horizontal and vertical scroll views, but instead use the method above so that when the user touches the invisible scroll view you translate those touches to the scroll views below that are locked down to respond to only the offset of the invisible scroll view. As you've stated in your comment, there's probably more to this answer for your individual needs, but this is the basic "foundation" that will give you the effect that you probably want.
Swift:
Same method as above, but here's the function that you'll need:
func scrollViewDidScroll(scrollView: UIScrollView!) {
verticalScrollView.contentOffset = CGPointMake(0, invisibleScrollView.contentOffset.y)
horizontalScrollView.contentOffset = CGPointMake(invisibleScrollView.contentOffset.x, 0)
}
I essentially want a UITextView to scroll vertically but not ever be able to accidentally scroll the UIPageViewController when the gesture occurs within the UITextView.
I do still want the page turning gesture to work along the edges though:
I want to implement a paging UIScrollView that will scroll in both directions (vertically, and horizontally). The default behaviour of UIScrollView is to be one, or the other. Not both.
Is it possible to implement a UIScrollView that would allow paging in both directions? For example, the user would be able to scroll left and up for the previous page with paging in UIScrollView, right and down for the next page.
I can use UIScrollView to implement scrolling horizontally with paging but I can't see how UIScrollView can be scrolled in both directions while paging. How could I implement this behaviour? UIScrollView ? or CAScrollLayer? or anything else?
Any suggestion are appreciated.
You seem to be talking about two different things here: scrolling and paging.
Scrolling is just flicking around to move the screen's viewport over a view that is larger than the screen itself. Normally you set this up by giving a UIScrollView a subview that is larger than it, and setting the UIScrollView's contentSize to the size of the larger thing.
There's nothing magic about it. If contentSize is taller than the UIScrollView's size, the scroll view will scroll vertically. If contentSize is wider, it will scroll horizontally. If contentSize is both taller and wider, then you'll scroll both vertically and horizontally.
Paging is a different matter. To know how to make it so "you can scroll left and up for the previous page", we'll need to know what a page looks like in your program, how it's represented, etc. From the small description you're giving, I think UIScrollView probably isn't the way to go. Instead, look into UIPanGestureRecognizer and some sort of animated transition between views?
But if that doesn't help, you'll need to give us some more details to work with.
I have seen many similar questions to this but none help me exactly. I have many vertical UISliders filling a wide UIScrollView. If I drag the thumb on the slider I want the horizontal UIScrollView to be locked and only allow vertical sliding. If I drag the UISlider other than on the thumb I want the horizontal UIScrollView scrolling. It currently works if I touch the thumb before dragging.
I have overridden touchesShouldCancelInContentView in the scroll view but never hit the breakpoint in that method.
I had to override touchesShouldBegin:withEvent:inContentView and also set delaysContentTouches to NO to intercept the touch message.
I have a UIScrollView that takes up a portion of the screen, and its basic functions (scrolling and zooming) work just fine. For example, I can grab the scrollview with two fingers, and scroll left and right, while zooming in or out on the content.
I want to add the ability to move the UIScrollView up and down at the same time. Please note that I do not want to move the content of the scroll view, but the scroll view itself.
I have attempted to add a UIPinchGestureRecognizer to the UIScrollView which has allowed me to grab and move the scroll view, but I then lose the zoom and scrolling. It seems that adding a gesture recognizer makes all of the default actions stop (also, when I call setZoomScale, I get an EXC_BAD_ACCESS).
I've also considered checking touches, but touches on the UIScrollView aren't registered with the main view so I can't change its location using those values.
Does anyone have any suggestions on how this might be accomplished?