国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS中PNChart與UITableView的聯動示例詳解

2020-07-26 02:25:48
字體:
來源:轉載
供稿:網友

前言

在開發中,特別是銷售企業內部使用的APP,可能會用到數據匯總,使用到圖表的功能!本文主要給大家介紹了關于iOS中PNChart與UITableView聯動的相關內容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧

效果圖


1.點擊chart,tableView對應模塊高亮

PNChart提供了一個代理方法,用來處理用戶的點擊事件:

#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.點擊cell,chart對應模塊高亮

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

思路:

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

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

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

發現它調用了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]; } }}

通過源代碼我們可以發現,用戶點擊chart的時候,將傳入的參數touchLocation轉換成了index,這個index正是代理方法userClickedOnPieIndexItem:所需要的參數。另外,chart的某一模塊高亮,實際上是addSublayer:,而這個sublayer的屬性也是由index決定的。所以,通過主動調用一個方法讓chart的某個模塊高亮,關鍵就是這個index。

這樣的話,就很簡單了。只需把didTouchAt :的后半段代碼提出來,就是我們需要的新方法了:

/** 某一模塊高亮 @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]; } }}

現在就可以實現點擊cell,chart對應模塊高亮了:

- (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]; // 對應的模塊高亮 [self.pieChart highlightItemWithIndex:indexPath.row];}

修改源碼注意事項

如果你的PNChart是手動拖進去的,修改源碼無所謂;

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

pod 'PNChart','0.8.9'

pod update的時候,若發現其版本是指定的版本,就不會更新了。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 定远县| 阿拉尔市| 宜宾市| 钦州市| 十堰市| 桃源县| 五家渠市| 林周县| 天镇县| 宣化县| 永新县| 香河县| 尖扎县| 平顶山市| SHOW| 微山县| 兴仁县| 南召县| 类乌齐县| 云和县| 台江县| 思茅市| 莱西市| 日照市| 商河县| 宿州市| 大城县| 朝阳区| 佛冈县| 平武县| 阳西县| 潞西市| 孙吴县| 海门市| 汝州市| 乌什县| 衡水市| 平谷区| 武汉市| 永修县| 徐汇区|