andi

Interactive Pie, and delegation protocol improvement

@@ -29,4 +29,5 @@ @@ -29,4 +29,5 @@
29 29
30 30
31 - (void)userClickedOnPieIndexItem:(NSInteger)pieIndex; 31 - (void)userClickedOnPieIndexItem:(NSInteger)pieIndex;
  32 +- (void)didUnselectPieItem;
32 @end 33 @end
@@ -21,7 +21,7 @@ @@ -21,7 +21,7 @@
21 @property (nonatomic) UIView *contentView; 21 @property (nonatomic) UIView *contentView;
22 @property (nonatomic) CAShapeLayer *pieLayer; 22 @property (nonatomic) CAShapeLayer *pieLayer;
23 @property (nonatomic) NSMutableArray *descriptionLabels; 23 @property (nonatomic) NSMutableArray *descriptionLabels;
24 - 24 +@property (strong, nonatomic) CAShapeLayer *sectorHighlight;
25 25
26 - (void)loadDefault; 26 - (void)loadDefault;
27 27
@@ -102,6 +102,7 @@ @@ -102,6 +102,7 @@
102 102
103 CGFloat radius = _innerCircleRadius + (_outerCircleRadius - _innerCircleRadius) / 2; 103 CGFloat radius = _innerCircleRadius + (_outerCircleRadius - _innerCircleRadius) / 2;
104 CGFloat borderWidth = _outerCircleRadius - _innerCircleRadius; 104 CGFloat borderWidth = _outerCircleRadius - _innerCircleRadius;
  105 +
105 CAShapeLayer *currentPieLayer = [self newCircleLayerWithRadius:radius 106 CAShapeLayer *currentPieLayer = [self newCircleLayerWithRadius:radius
106 borderWidth:borderWidth 107 borderWidth:borderWidth
107 fillColor:[UIColor clearColor] 108 fillColor:[UIColor clearColor]
@@ -252,17 +253,57 @@ @@ -252,17 +253,57 @@
252 }]; 253 }];
253 } 254 }
254 255
  256 +- (void)didTouchAt:(CGPoint)touchLocation
  257 +{
  258 + CGPoint circleCenter = CGPointMake(_contentView.bounds.size.width/2, _contentView.bounds.size.height/2);
  259 +
  260 + CGFloat distanceFromCenter = sqrtf(powf((touchLocation.y - circleCenter.y),2) + powf((touchLocation.x - circleCenter.x),2));
  261 +
  262 + if (distanceFromCenter < _innerCircleRadius) {
  263 + if ([self.delegate respondsToSelector:@selector(didUnselectPieItem)]) {
  264 + [self.delegate didUnselectPieItem];
  265 + }
  266 + [self.sectorHighlight removeFromSuperlayer];
  267 + return;
  268 + }
  269 +
  270 + CGFloat percentage = [self findPercentageOfAngleInCircle:circleCenter fromPoint:touchLocation];
  271 + int index = 0;
  272 + while (percentage > [self endPercentageForItemAtIndex:index]) {
  273 + index ++;
  274 + }
  275 +
  276 + if ([self.delegate respondsToSelector:@selector(userClickedOnPieIndexItem:)]) {
  277 + [self.delegate userClickedOnPieIndexItem:index];
  278 + }
  279 +
  280 + if (self.sectorHighlight) {
  281 + [self.sectorHighlight removeFromSuperlayer];
  282 + }
  283 + PNPieChartDataItem *currentItem = [self dataItemForIndex:index];
  284 +
  285 + CGFloat red,green,blue,alpha;
  286 + UIColor *old = currentItem.color;
  287 + [old getRed:&red green:&green blue:&blue alpha:&alpha];
  288 + alpha /= 2;
  289 + UIColor *newColor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
  290 +
  291 + CGFloat startPercnetage = [self startPercentageForItemAtIndex:index];
  292 + CGFloat endPercentage = [self endPercentageForItemAtIndex:index];
  293 + self.sectorHighlight = [self newCircleLayerWithRadius:_outerCircleRadius + 5
  294 + borderWidth:10
  295 + fillColor:[UIColor clearColor]
  296 + borderColor:newColor
  297 + startPercentage:startPercnetage
  298 + endPercentage:endPercentage];
  299 + [_contentView.layer addSublayer:self.sectorHighlight];
  300 +}
  301 +
255 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 302 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
256 { 303 {
257 for (UITouch *touch in touches) { 304 for (UITouch *touch in touches) {
258 CGPoint touchLocation = [touch locationInView:_contentView]; 305 CGPoint touchLocation = [touch locationInView:_contentView];
259 - CGPoint circleCenter = CGPointMake(_contentView.bounds.size.width/2, _contentView.bounds.size.height/2); 306 + [self didTouchAt:touchLocation];
260 - CGFloat percentage = [self findPercentageOfAngleInCircle:circleCenter fromPoint:touchLocation];  
261 - int index = 0;  
262 - while (percentage > [self endPercentageForItemAtIndex:index]) {  
263 - index ++;  
264 - }  
265 - [self.delegate userClickedOnPieIndexItem:index];  
266 } 307 }
267 } 308 }
268 309