最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS中PNChart與UITableView的聯(lián)動(dòng)示例詳解

 更新時(shí)間:2018年07月11日 11:13:28   作者:無(wú)夜之星辰  
PNChart是個(gè)界面很漂亮的圖表第三方庫(kù),UITableView則不用過(guò)多介紹了,各位iOS開發(fā)者們都知道,下面這篇文章主要給大家介紹了關(guān)于iOS中PNChart與UITableView的聯(lián)動(dòng)的相關(guān)資料,需要的朋友可以參考下

前言

在開發(fā)中,特別是銷售企業(yè)內(nèi)部使用的APP,可能會(huì)用到數(shù)據(jù)匯總,使用到圖表的功能!本文主要給大家介紹了關(guān)于iOS中PNChart與UITableView聯(lián)動(dòng)的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧

效果圖


1.點(diǎn)擊chart,tableView對(duì)應(yīng)模塊高亮

PNChart提供了一個(gè)代理方法,用來(lái)處理用戶的點(diǎn)擊事件:

#pragma mark - PNChart Delegate

- (void)userClickedOnPieIndexItem:(NSInteger)pieIndex {
 for (int i = 0; i < self.model.department_sale.count; i++) {
 CQSaleDetailDepartmentItemModel *model = self.model.department_sale[i];
 model.selected = (i == pieIndex);
 }
 [self.tableView reloadData];
 [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:pieIndex inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}

2.點(diǎn)擊cell,chart對(duì)應(yīng)模塊高亮

PNChart并未提供相應(yīng)方法讓某一模塊高亮,怎么辦?

思路:

雖然PNChart未直接提供讓某一模塊高亮的方法,但是我們可以從用戶點(diǎn)擊模塊高亮那部分代碼入手,看看用戶點(diǎn)擊到模塊高亮是怎樣一個(gè)過(guò)程。

1.在PNPieChart.m里面找到touchesBegan方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 for (UITouch *touch in touches) {
 CGPoint touchLocation = [touch locationInView:_contentView];
 [self didTouchAt:touchLocation];
 }
}

發(fā)現(xiàn)它調(diào)用了didTouchAt:方法。

2.分析didTouchAt:

- (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];
 }
 }
}

通過(guò)源代碼我們可以發(fā)現(xiàn),用戶點(diǎn)擊chart的時(shí)候,將傳入的參數(shù)touchLocation轉(zhuǎn)換成了index,這個(gè)index正是代理方法userClickedOnPieIndexItem:所需要的參數(shù)。另外,chart的某一模塊高亮,實(shí)際上是addSublayer:,而這個(gè)sublayer的屬性也是由index決定的。所以,通過(guò)主動(dòng)調(diào)用一個(gè)方法讓chart的某個(gè)模塊高亮,關(guān)鍵就是這個(gè)index。

這樣的話,就很簡(jiǎn)單了。只需把didTouchAt :的后半段代碼提出來(lái),就是我們需要的新方法了:

/**
 某一模塊高亮

 @param index 高亮模塊的index
 */
- (void)highlightItemWithIndex:(NSInteger)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:@"%ld", (long)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];
 }
 }
}

現(xiàn)在就可以實(shí)現(xiàn)點(diǎn)擊cell,chart對(duì)應(yīng)模塊高亮了:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 for (int i = 0; i < self.model.department_sale.count; i++) {
 CQSaleDetailDepartmentItemModel *model = self.model.department_sale[i];
 model.selected = (i == indexPath.row);
 }
 [self.tableView reloadData];
 [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
 // 對(duì)應(yīng)的模塊高亮
 [self.pieChart highlightItemWithIndex:indexPath.row];
}

修改源碼注意事項(xiàng)

如果你的PNChart是手動(dòng)拖進(jìn)去的,修改源碼無(wú)所謂;

但如果是用CocoaPods管理的話,就要注意一下了:pod update的時(shí)候會(huì)覆蓋你寫的代碼。為避免這種事情發(fā)生,你可以指定庫(kù)的版本,如:

pod 'PNChart','0.8.9'

pod update的時(shí)候,若發(fā)現(xiàn)其版本是指定的版本,就不會(huì)更新了。

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論

康乐县| 白河县| 张家港市| 宁乡县| 文水县| 韶山市| 朝阳区| 汶上县| 陵水| 上高县| 桐柏县| 阳朔县| 正安县| 永宁县| 莒南县| 南平市| 靖远县| 新乐市| 双柏县| 乌拉特后旗| 台湾省| 彩票| 绥江县| 黎平县| 双牌县| 连城县| 获嘉县| 松江区| 东乡| 石屏县| 芦山县| 临汾市| 洱源县| 绍兴市| 宜宾市| 岱山县| 仲巴县| 台安县| 习水县| 阳高县| 方城县|