Kevin

Merge pull request #49 from orlandoaleman/master

Detects touches on the bars and, if implemented, calls to the delegate
@@ -7,6 +7,7 @@ @@ -7,6 +7,7 @@
7 // 7 //
8 8
9 #import <UIKit/UIKit.h> 9 #import <UIKit/UIKit.h>
  10 +#import "PNChartDelegate.h"
10 11
11 #define chartMargin 10 12 #define chartMargin 10
12 #define xLabelMargin 15 13 #define xLabelMargin 15
@@ -32,4 +33,5 @@ @@ -32,4 +33,5 @@
32 @property (nonatomic) UIColor *barBackgroundColor; 33 @property (nonatomic) UIColor *barBackgroundColor;
33 @property (nonatomic) BOOL showLabel; 34 @property (nonatomic) BOOL showLabel;
34 35
  36 +@property (nonatomic, retain) id<PNChartDelegate> delegate;
35 @end 37 @end
@@ -119,6 +119,7 @@ @@ -119,6 +119,7 @@
119 bar.backgroundColor = _barBackgroundColor; 119 bar.backgroundColor = _barBackgroundColor;
120 bar.barColor = [self barColorAtIndex:index]; 120 bar.barColor = [self barColorAtIndex:index];
121 bar.grade = grade; 121 bar.grade = grade;
  122 + bar.tag = index;
122 [_bars addObject:bar]; 123 [_bars addObject:bar];
123 [self addSubview:bar]; 124 [self addSubview:bar];
124 125
@@ -149,4 +150,26 @@ @@ -149,4 +150,26 @@
149 } 150 }
150 151
151 152
  153 +#pragma mark - Touch detection
  154 +
  155 +- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  156 +{
  157 + [self touchPoint:touches withEvent:event];
  158 + [super touchesBegan:touches withEvent:event];
  159 +}
  160 +
  161 +
  162 +- (void)touchPoint:(NSSet *)touches withEvent:(UIEvent *)event
  163 +{
  164 + //Get the point user touched
  165 + UITouch *touch = [touches anyObject];
  166 + CGPoint touchPoint = [touch locationInView:self];
  167 + UIView *subview = [self hitTest:touchPoint withEvent:nil];
  168 +
  169 + if ([subview isKindOfClass:[PNBar class]] && [self.delegate respondsToSelector:@selector(userClickedOnBarCharIndex:)]) {
  170 + [self.delegate userClickedOnBarCharIndex:subview.tag];
  171 + }
  172 +}
  173 +
  174 +
152 @end 175 @end
@@ -22,5 +22,10 @@ @@ -22,5 +22,10 @@
22 */ 22 */
23 - (void)userClickedOnLineKeyPoint:(CGPoint)point lineIndex:(NSInteger)lineIndex andPointIndex:(NSInteger)pointIndex; 23 - (void)userClickedOnLineKeyPoint:(CGPoint)point lineIndex:(NSInteger)lineIndex andPointIndex:(NSInteger)pointIndex;
24 24
  25 +/**
  26 + * When user click on a chart bar
  27 + *
  28 + */
  29 +- (void)userClickedOnBarCharIndex:(NSInteger)barIndex;
25 30
26 @end 31 @end
@@ -111,6 +111,8 @@ @@ -111,6 +111,8 @@
111 [barChart setStrokeColors:@[PNGreen,PNGreen,PNRed,PNGreen,PNGreen,PNYellow,PNGreen]]; 111 [barChart setStrokeColors:@[PNGreen,PNGreen,PNRed,PNGreen,PNGreen,PNYellow,PNGreen]];
112 [barChart strokeChart]; 112 [barChart strokeChart];
113 113
  114 + barChart.delegate = self;
  115 +
114 [viewController.view addSubview:barChartLabel]; 116 [viewController.view addSubview:barChartLabel];
115 [viewController.view addSubview:barChart]; 117 [viewController.view addSubview:barChart];
116 118
@@ -148,4 +150,9 @@ @@ -148,4 +150,9 @@
148 NSLog(@"Click on line %f, %f, line index is %d",point.x, point.y, (int)lineIndex); 150 NSLog(@"Click on line %f, %f, line index is %d",point.x, point.y, (int)lineIndex);
149 } 151 }
150 152
  153 +- (void)userClickedOnBarCharIndex:(NSInteger)barIndex
  154 +{
  155 + NSLog(@"Click on bar %@", @(barIndex));
  156 +}
  157 +
151 @end 158 @end