iOS中PNChart与UITableView的联动示例详解

  - (void)didTouchAt:(CGPoint)touchLocation

  {

  CGPoint circleCenter = CGPointMake(_contentView.bounds.size.width/2, _contentView.bounds.size.height/2);

  CGFloat distanceFromCenter = sqrtf(powf((touchLocation.y - circleCenter.y),2) + powf((touchLocation.x - circleCenter.x),2));

  if (distanceFromCenter < _innerCircleRadius) {

  if ([self.delegate respondsToSelector:@selector(didUnselectPieItem)]) {

  [self.delegate didUnselectPieItem];

  }

  [self.sectorHighlight removeFromSuperlayer];

  return;

  }

  CGFloat percentage = [self findPercentageOfAngleInCircle:circleCenter fromPoint:touchLocation];

  int index = 0;

  while (percentage > [self endPercentageForItemAtIndex:index]) {

  index ++;

  }

  if ([self.delegate respondsToSelector:@selector(userClickedOnPieIndexItem:)]) {

  [self.delegate userClickedOnPieIndexItem:index];

  }

  if (self.shouldHighlightSectorOnTouch)

  {

  if (!self.enableMultipleSelection)

  {

  if (self.sectorHighlight)

  [self.sectorHighlight removeFromSuperlayer];

  }

  PNPieChartDataItem *currentItem = [self dataItemForIndex:index];

  CGFloat red,green,blue,alpha;

  UIColor *old = currentItem.color;

  [old getRed:&red green:&green blue:&blue alpha:&alpha];

  alpha /= 2;

  UIColor *newColor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];

  CGFloat startPercentage = [self startPercentageForItemAtIndex:index];

  CGFloat endPercentage = [self endPercentageForItemAtIndex:index];

  self.sectorHighlight = [self newCircleLayerWithRadius:_outerCircleRadius + 5

  borderWidth:10

  fillColor:[UIColor clearColor]

  borderColor:newColor

  startPercentage:startPercentage

  endPercentage:endPercentage];

  if (self.enableMultipleSelection)

  {

  NSString *dictIndex = [NSString stringWithFormat:@"%d", index];

  CAShapeLayer *indexShape = [self.selectedItems valueForKey:dictIndex];

  if (indexShape)

  {

  [indexShape removeFromSuperlayer];

  [self.selectedItems removeObjectForKey:dictIndex];

  }

  else

  {

  [self.selectedItems setObject:self.sectorHighlight forKey:dictIndex];

  [_contentView.layer addSublayer:self.sectorHighlight];

  }

  }

  else

  {

  [_contentView.layer addSublayer:self.sectorHighlight];

  }

  }

  }

您可能感兴趣的文章:

相关文章