通过RAC封装SegmentView

近来在项目中需要用到SegmentView。感觉设置起来非常不方便。所以打算自己封装一个SegmentView.
使用到第三方库为RAC;
最终效果如下


segmentView效果.gif

这个项目里面核心的一点是如何将自己封装的SegmentView上的点击信号传送出来。在这里使用了RACSignal。
主要文件介绍
QXSegmentView(自定义的SegmentView)
ViewController(添加SegmentView的控制器)
点击的时候再QXSegmentView中调用

- (void)setSelectWithIndex:(NSInteger)select{
    if (self.currentSelect && self.currentSelect == select){
        return;
    }
    UIImage *imageClick = [UIImage imageNamed:self.arrayImageName[select]];
    [self.baseImageView setImage:imageClick];
    UILabel *labelSelect = self.arrayLabel[select];
    [labelSelect setTextColor:[UIColor grayColor]];
    UILabel *labelCurrent = self.arrayLabel[_currentSelect];
    [labelCurrent setTextColor:[UIColor blackColor]];
    _currentSelect = select;
    [self.subject sendNext:@(select)];
}

然后在添加SegmentView的ViewController中去订阅此信号。实现代码为

- (void)initRAC{
    [self.segmentView.signalClick subscribeNext:^(id x) {
        NSInteger index = [(NSNumber *)x integerValue];
        UIViewController *controller = self.arrayController[index];
        [self.view addSubview:controller.view];
        if (_currentSelect == index){
            return ;
        }
        UIViewController *currentController = self.arrayController[_currentSelect];
        [currentController.view removeFromSuperview];
        _currentSelect = index;
    }];
}

其他的就是在SegmentView内部却设置QXSegmentView的UI样式了。这些可以自由发挥。
此项目在gitHub的地址为:https://github.com/wangqingxue/QXSegmentView
感谢观看。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • RAC使用测试Demo下载:github.com/FuWees/WPRACTestDemo 1.ReactiveC...
    FuWees阅读 6,535评论 3 10
  • WebSocket-Swift Starscream的使用 WebSocket 是 HTML5 一种新的协议。它实...
    香橙柚子阅读 24,233评论 8 184
  • 从某种意义上来看,世间一切,都是遇见。就像,冷遇见暖,就有了雨;春遇见冬,就有了岁月;天遇见地,有了永恒;人遇见人...
    花开不散清风自来阅读 152评论 0 0
  • 我的精神和身体状态越来越不好了,我能够非常清楚的意识到。我刻意减少自己的食量,将中餐和晚餐的饭量减少到原来的一半。...
    安小姐的日记簿阅读 237评论 0 0
  • 写这篇文字的时候,正好是阴历的生日,许愿的时候希望来年越来越好,没有明确的目的,有些太过的贪心。说的具体些呢,我希...
    函数阅读 290评论 1 1