Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable scroll in a specific subview of a UIScrollView

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.

like image 303
Desmond Avatar asked Nov 24 '14 08:11

Desmond


People also ask

Is there a way to scroll vertically in UIScrollView?

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.

What is Scroll View in UITableView?

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?

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.

How do I add a scroll view to a view controller?

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.


2 Answers

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:

Final 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.

like image 135
Desmond Avatar answered Oct 18 '22 04:10

Desmond


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.

like image 29
Dev Avatar answered Oct 18 '22 03:10

Dev