0
点赞
收藏
分享

微信扫一扫

iOS tableView 右侧索引视图状态获取

凉夜lrs 2022-10-25 阅读 156


需求

一图胜千言!

iOS tableView 右侧索引视图状态获取_ico

在​​触摸​​​右侧索引栏时
需要展示对应的​​​组号名称​​​ 手指​​离开​​时消失

实现

通过​​Xcode​​​查看​​视图层次结构​​​ 右侧索引视图的继承关系
是这样的:

UITableViewIndex
UIControl
UIView
UIResponder
NSObject

很显然​​UITableViewIndex​​​ 是私有的
但是​​​UIControl​​ 是公开的

于是
通过遍历​​​tableView​​的子视图来获取它

- (void)reloadData
{
[_tableView reloadData];

for (UIView *view in _tableView.subviews) {
if ([view isKindOfClass:[UIControl class]]) {
_indexControl = (UIControl *)view;
}
}
}

接着
在代理方法里面
处理相关逻辑就OK了

/// 点击右侧索引时的代理方法
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
_sectionLabel.text = title; // 显示 组号标题 的Label
if (_sectionLabel.hidden) { // 隐藏了,显示它
_sectionLabel.hidden = NO;
}

// 延迟 1 秒隐藏
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// isTracking: A Boolean value indicating whether the control is currently tracking touch events.
if (!_indexControl.isTracking) { // 没有触摸时,隐藏它
_sectionLabel.hidden = YES;
}
});

return index;
}

一个有趣的点赞动画

​​https://github.com/xjh093/JHLikeButton​​


举报

相关推荐

0 条评论