I have a UIScrollView
which contains many subviews. One of the subview is designed to show a linechart, so the user may need to drag horizontally. The truth is when I mean to drag my finger horizontally, the vertical scroll of the UIScrollView
is easily activated. Now I want to disable the vertical scroll in the subview of chart, and leave it active in the rest parts.
I've tried to add a UIPanGestureRecognizer
to my chart subview. It did disabled the vertical scroll but the horizontal scroll is disabled too. I know I can write codes in the handler of the gesture recognizer to tell vertical or horizontal scroll i needed. But the horizontal scroll is actually managed by the subview's controller, which is a third-party library (JBChartView
to be specific). I want to know whether there is a simple way to solve this problem.
Many thanks.
The principles are the same for vertical scrolling, though. The UIScrollView should only use one subview. This is a 'UIView' that serves as the content view to hold everything you wish to scroll. Make the content view and the scroll view's parent have equal heights for horizontal scrolling.
A view that allows the scrolling and zooming of its contained views. UIScrollView is the superclass of several UIKit classes, including UITableView and UITextView. A scroll view is a view with an origin that’s adjustable over the content view.
How to disable scrolling temporarily using JavaScript? Scrolling can be disabled using JavaScript using 2 methods: The window.onscroll event fires when the window has been scrolled. Overriding this function and setting it to a fixed position every time the scroll happens will effectively disable the scroll effect.
Add a UIScrollView and pin all four sides to the root view of the view controller. Add a UIView as a subview to the scroll view. This is key. Don't try to add lots of subviews to the scroll view. Just add a single UIView. This will be your content view for the other views you want to scroll.
Thanks to Dev and Astoria, I have solved this problem. Now I want post my solution here in case there will be someone who meet the same problem as mine.
Here is the result:
Because the Horizontal Only View is not a scrollView (actually it's a JBLineChartView), the easiest scrollViewDidScroll way doesn't help.
We still need GestureRecognizer to achieve this goal, but in a more basic way, just as Dev said, -touchesBegan methods. Sadly UIScrollView will not respond to this kind of touch, we need write a category for it, as code below:
@implementation UIScrollView (UITouchEvent)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self nextResponder] touchesBegan:touches withEvent:event];
[super touchesBegan:touches withEvent:event];
self.scrollEnabled = YES;//This is the only line that you need to customize
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[[self nextResponder] touchesMoved:touches withEvent:event];
[super touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[[self nextResponder] touchesEnded:touches withEvent:event];
[super touchesEnded:touches withEvent:event];
}
@end
Ha, then it works!
BTW....This is my first question on StackOverflow, and it's a really nice tour.
Use -touchesBegan of UIResponder class. If the touch is from your subview(which you don't want to scroll). Disable scrolling of the scrollview. if -touchesEnded or -touchesCancelled is called for that view. enable scrolling of your scrollview.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With