iOS造轮子 - UITableView字母索引条 - 图1

最近重构项目的通信录页面,旧版本的索引条相当丑陋,找了下轮子又找不到,没办法,只能自己造了。发现微信的通讯录索引条样式还不错,照着写了一个,顺便添加了震动效果(Impact Feedback)。

首先看一下效果:

单击索引条时

iOS造轮子 - UITableView字母索引条 - 图2

滑动tableView时

iOS造轮子 - UITableView字母索引条 - 图3

在索引条上滑动时

iOS造轮子 - UITableView字母索引条 - 图4

实现原理

主要分为以下几步:

1、每一个索引,都是一个label,把所有label都竖直排列在一个父view中。这里没有使用重用池,主要考虑到一般的索引条,字母数量也不会太多。

  1. _labelArr = [NSMutableArray new];
  2. for (int i = 0; i < indexes.count; i++) {
  3. CGFloat y = (_sectionHeight * i);
  4. UILabel *alphaLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
  5. alphaLabel.textAlignment = NSTextAlignmentCenter;
  6. alphaLabel.text = [[indexes objectAtIndex:i] uppercaseString];
  7. alphaLabel.font = [UIFont boldSystemFontOfSize:10.0];
  8. alphaLabel.backgroundColor = [UIColor clearColor];
  9. alphaLabel.textColor = self.textColor;
  10. alphaLabel.layer.cornerRadius = width * 0.5;
  11. alphaLabel.clipsToBounds = YES;
  12. [self addSubview:alphaLabel];
  13. [_labelArr addObject:alphaLabel];
  14. }

2、当在父view中触摸时,通过touchMoved等方法处理触摸点和索引label之间的关系,如果触摸点落在label范围之内,则将label高亮选中。

  1. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  2. {
  3. [super touchesMoved:touches withEvent:event];
  4. CGPoint touchPoint = [[[event touchesForView:self] anyObject] locationInView:self];
  5. [self toSelectTitle:touchPoint];
  6. }
  7. // 通过touchPoint找到对应的索引label
  8. - (void)toSelectTitle:(CGPoint)touchPoint
  9. {
  10. if(touchPoint.x <= 0 ||
  11. touchPoint.y <= 0 ||
  12. touchPoint.x >= self.bounds.size.width ||
  13. touchPoint.y >= self.bounds.size.height) return;
  14. __block NSString *title;
  15. __block NSInteger index = 0;
  16. [_labelArr enumerateObjectsUsingBlock:^(__kindof UILabel * _Nonnull subview, NSUInteger idx, BOOL * _Nonnull stop) {
  17. if(touchPoint.y < CGRectGetMaxY(subview.frame)) {
  18. _preLabel.backgroundColor = [UIColor clearColor];
  19. _preLabel.textColor = _textColor;
  20. subview.backgroundColor = _selectedBackgroundColor;
  21. subview.textColor = _selectedTextColor;
  22. _preLabel = subview;
  23. index = idx;
  24. title = subview.text;
  25. *stop = YES;
  26. }
  27. }];
  28. ......
  29. }

3、添加震动效果,并触发点击回调

  1. //震动
  2. if (@available(iOS 10.0, *)) {
  3. UIImpactFeedbackGenerator *gen = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight];
  4. [gen prepare];
  5. [gen impactOccurred];
  6. }
  7. //回调
  8. if (_delegate && [_delegate conformsToProtocol:@protocol(TTIndexBarDelegate)]) {
  9. [_delegate indexDidChanged:self index:index title:title];
  10. }

如何使用

默认样式:

  1. self.indexBar = [[TTIndexBar alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 30, 0, 30, self.view.frame.size.height)];
  2. self.indexBar.delegate = self;
  3. [self.view addSubview:self.indexBar];
  4. [self.indexBar setIndexes:[NSMutableArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J", nil]];

自定义样式:

  1. self.indexBar = [[TTIndexBar alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 30, 0, 30, self.view.frame.size.height)];
  2. self.indexBar.delegate = self;
  3. [self.view addSubview:self.indexBar];
  4. //Custom property
  5. self.indexBar.textColor = [UIColor redColor];
  6. self.indexBar.selectedTextColor = [UIColor greenColor];
  7. self.indexBar.selectedBackgroundColor = [UIColor yellowColor];
  8. self.indexBar.detailViewDrawColor = [UIColor cyanColor];
  9. self.indexBar.detailViewTextColor = [UIColor orangeColor];
  10. [self.indexBar setIndexes:[NSMutableArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J", nil]];

Delegate:

  1. - (void)indexDidChanged:(TTIndexBar *)indexBar index:(NSInteger)index title:(NSString *)title;

GitHub

TTIndexBar

资料推荐

如果你正在跳槽或者正准备跳槽不妨动动小手,添加一下咱们的交流群1012951431来获取一份详细的大厂面试资料为你的跳槽多添一份保障。

iOS造轮子 - UITableView字母索引条 - 图5