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,18 +253,58 @@ @@ -252,18 +253,58 @@
252 }]; 253 }];
253 } 254 }
254 255
255 -- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 256 +- (void)didTouchAt:(CGPoint)touchLocation
256 { 257 {
257 - for (UITouch *touch in touches) {  
258 - CGPoint touchLocation = [touch locationInView:_contentView];  
259 CGPoint circleCenter = CGPointMake(_contentView.bounds.size.width/2, _contentView.bounds.size.height/2); 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 +
260 CGFloat percentage = [self findPercentageOfAngleInCircle:circleCenter fromPoint:touchLocation]; 270 CGFloat percentage = [self findPercentageOfAngleInCircle:circleCenter fromPoint:touchLocation];
261 int index = 0; 271 int index = 0;
262 while (percentage > [self endPercentageForItemAtIndex:index]) { 272 while (percentage > [self endPercentageForItemAtIndex:index]) {
263 index ++; 273 index ++;
264 } 274 }
  275 +
  276 + if ([self.delegate respondsToSelector:@selector(userClickedOnPieIndexItem:)]) {
265 [self.delegate userClickedOnPieIndexItem:index]; 277 [self.delegate userClickedOnPieIndexItem:index];
266 } 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 +
  302 +- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  303 +{
  304 + for (UITouch *touch in touches) {
  305 + CGPoint touchLocation = [touch locationInView:_contentView];
  306 + [self didTouchAt:touchLocation];
  307 + }
267 } 308 }
268 309
269 - (CGFloat) findPercentageOfAngleInCircle:(CGPoint)center fromPoint:(CGPoint)reference{ 310 - (CGFloat) findPercentageOfAngleInCircle:(CGPoint)center fromPoint:(CGPoint)reference{