KenTagSelector 1.0.4

KenTagSelector 1.0.4

Maintained by Ken_Hanks.



  • By
  • ken-hanks

KenTagSelector

A tag selector written by Objective-C. (一个类似网易栏目选择器的标签选择界面)

功能特性

  • 用户可单选栏目或将栏目加入选择列表。
  • 可设置固定栏目,固定栏目不能被删除。
  • 已选栏目列表中,长按Tag支持拖拽排序。
  • 已适配iOS13的暗黑模式。

选择栏目 编辑栏目界面 已适配暗黑模式 已适配暗黑模式 动画效果

安装方法

使用CocoaPods (推荐)

  1. 在 Podfile 中添加一行 pod 'KenTagSelector'。
  2. 执行 pod install 或 pod update。

手工安装

  1. 点击"Code"按钮,选择"Download ZIP",将代码下载到本地。
  2. 将ZIP文件解压,进入解压后的目录,用Xcode打开KenTagSelector.xcodeproj工程。
  3. 按Command+B编译,编译成功后,在Products目录下会生成KenTagSelector.framework。
  4. 在KenTagSelector.framework上点击鼠标右键,选“Show in Finder“,即可找到编译后的framework文件。
  5. 将KenTagSelector.framework插入到您的项目中。

使用说明

接入方式很简单,所以直接贴代码了。在需要展示栏目选择器的ViewController里像下面这样写就可以了。 选择结果通过2个Block返回(分别对应单选和多选)。


#import "ViewController.h"
#import <KenTagSelector/KenTagSelector.h>

@interface ViewController ()
{
    __block NSMutableArray *selectedTagArray;
    __block NSMutableArray *otherTagArray;
    NSArray     *residentArray;
    NSString    *focusTitle;
}

@property (weak, nonatomic) IBOutlet UILabel *labelSelected;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //已选栏目列表
    selectedTagArray = @[@"关注",@"头条",@"抗疫",@"北京",@"科技",@"汽车",@"社会",@"军事",@"知否",@"历史",@"要闻",@"财经",@"军事"].mutableCopy;
    
    //备选栏目列表
    otherTagArray = @[@"独家",@"航空",@"娱乐",@"影视",@"音乐",@"股票",@"体育",@"CBA",@"冬奥",@"手机",@"数码",@"房产",@"游戏",@"旅游",@"健康",@"亲子",@"时尚",@"艺术",@"星座",@"段子",@"跟帖",@"图片",@"萌宠",@"圈子"].mutableCopy;
    
    //固定栏目列表(可选),注意:这个列表里的文字必须在“已选栏目列表”中存在,否则设了也没有用
    residentArray = @[@"关注", @"头条"];
 
    //焦点栏目(可选)
    focusTitle = @"头条";
}

//“选择栏目列表”按钮被点击的处理
- (IBAction)onBtnSelectClicked:(id)sender {
    
    //创建栏目选择器,输入已选栏目列表和备选栏目列表
    TagSelectorVC *selectorVC = [[TagSelectorVC alloc] initWithSelectedTags:selectedTagArray andOtherTags:otherTagArray];
    
    //设置固定栏目(可选步骤)
    selectorVC.residentTagStringArray = residentArray;
    
    //设置焦点栏目(可选步骤)
    selectorVC.focusTitle = focusTitle;
    
    //弹出栏目选择界面
    [self presentViewController:selectorVC animated:YES completion:^{}];
    
    //返回所有选中栏目
    __block  NSMutableString *strChannels = [NSMutableString new];
    selectorVC.choosedTags = ^(NSArray *selectedTags, NSArray *otherTags) {
        self->selectedTagArray = @[].mutableCopy;
        self->otherTagArray = @[].mutableCopy;
        for (Channel *channel in otherTags) {
            [self->otherTagArray addObject:channel.title];
        }
        for (Channel *channel in selectedTags) {
            [self->selectedTagArray addObject:channel.title];
            [strChannels appendString:channel.title];
            [strChannels appendString:@", "];
        }
        self->_labelSelected.text = strChannels;  //将选中的栏目在Label中显示
    };
    
    //用户点击了某个栏目的处理Block
    selectorVC.activeTag = ^(Channel *channel, NSInteger index) {
        [strChannels appendString:channel.title];
        self->_labelSelected.text = strChannels;  //将选中的栏目在Label中显示
        NSLog(@"The index of Active Tag is: %ld", index);
    };
}


@end