I have a LineChartView
from the Charts framework within a UIScrollView
. Values on the chart can be highlighted by panning around to see more details about the specific data points.
These are my goals for the overall behaviour of panning within my view controller:
- The scroll view should scroll if the user pans anywhere on the view controller. Even on the chart view, providing that the direction is vertical.
- The chart should only highlight values when the user scrolls on it horizontally and the scroll view should not react in this case.
Currently, the scroll view ignores scrolling when interacting with the chart completely and the chart reacts to vertical panning, which it should not, so while I'm close to a solution, it's not quite there.
Here is my current implementation...
- (void)viewDidLoad {
[super viewDidLoad];
NSArray<UIGestureRecognizer *> *gestureRecognizers = self.lineChartView.gestureRecognizers;
for (UIGestureRecognizer *gestureRecognizer in gestureRecognizers) {
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
gestureRecognizer.delegate = self;
self.lineChartViewPanGestureRecognizer = (UIPanGestureRecognizer *)gestureRecognizer;
}
}
UIPanGestureRecognizer *lineChartViewPanGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleLineChartViewPanGestureRecognizer:)];
[self.lineChartView addGestureRecognizer:lineChartViewPanGestureRecognizer];
}
- (void)handleLineChartViewPanGestureRecognizer:(UIPanGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
[self.lineChartView highlightValues:NULL];
}
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if (gestureRecognizer == self.lineChartViewPanGestureRecognizer && otherGestureRecognizer == self.scrollView.panGestureRecognizer) {
UIPanGestureRecognizer* panGestureRecognizer = (UIPanGestureRecognizer *)otherGestureRecognizer;
CGPoint velocity = [panGestureRecognizer velocityInView:self.view];
return fabs(velocity.y) > fabs(velocity.x);
}
return YES;
}
Anyone have any suggestions as to how I can keep the scroll view scrolling when the user pans vertically on the chart (which should itself ignore such panning)?