7

I have a ios-chart as a subview that takes up half the screen. When I pan up on any other subview it scrolls. But not when I pan on the chart. I tried setting:

[self.chart setDefaultTouchEventsEnabled:YES];
//and
[self.chart setScaleEnabled:NO];

It says in the documentation

defaultTouchEventsEnabled enables/disables default touch events to be handled. When disable, touches are not passed to parent views so scrolling inside a UIScrollView won’t work.

What can I do to enable scrolling when panning/dragging on the chart?

Community
  • 1
  • 1
Entrabiter
  • 752
  • 7
  • 13

3 Answers3

9

I was struggling with this as well. Since I needed the chart to be scrollable, @Entrabiter's solution didn't work for me. The only solution that worked for me, was assigning the delegate of the chart view's UIPanGestureRecognizer to my ViewController and implement UIGestureRecognizerDelegate.

This solution is in Swift, but it should also work fine in Objective-C.

class MyViewController: UIViewController, UIGestureRecognizerDelegate {
    // MARK: Outlets
    @IBOutlet weak var contentView: UIScrollView!
    @IBOutlet weak var myChart: LineChartView!

    override func viewDidLoad() {
        super.viewDidLoad()

        if let gestureRecognizers = myChart.gestureRecognizers {
            for gestureRecongnizer in gestureRecognizers {
                if gestureRecongnizer is UIPanGestureRecognizer {
                    gestureRecongnizer.delegate = self
                }
            }
        }
    }
}

The important part is to tell the gestureRecognizer to recognize both the scrollview's panGestureRecognizer as well as the chartview's panGestureRecognizer.

// MARK: UIGestureRecognizerDelegate
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    if otherGestureRecognizer == contentView.panGestureRecognizer {
        return true
    }

    return false
}
ergoon
  • 1,264
  • 9
  • 17
2

Set userInteractionEnabled on the chart view to NO.

rounak
  • 9,217
  • 3
  • 42
  • 59
  • That worked. But now I'm unable to tap on my points. Is there a way to have it both ways? – Entrabiter Jun 18 '15 at 15:00
  • @Entrabiter what chart library are you using? – rounak Jun 18 '15 at 15:34
  • 2
    @Entrabiter The reason this is happening is that the chart library is eating up all the touches. You'll somehow have to figure out a way to delay touches that go into the chart view to let the scroll view intercept them if you're making a drag gesture. – rounak Jun 18 '15 at 15:57
0

I think I figured it out... I removed all the UIPanGestureRecognizers from the ios-charts subview. This allowed for the scrollview to handle all the pan events.

for(UIGestureRecognizer *rec in self.chart.gestureRecognizers){
    if([rec isKindOfClass:[UIPanGestureRecognizer class]]){
        [self.chart removeGestureRecognizer:rec];
    }
}
Entrabiter
  • 752
  • 7
  • 13