时间选择滑动控件

近期接收一个老项目,据说是搁置了一段时间,现在又捡起来继续做,拿到项目也是醉了,32位,最低版本6.0,6和6p上就是自动拉伸,效果略显模糊,装在10.0以上的机子上会提示“该应用可能会使您的手机变慢”
开始了大刀阔斧的适配之路。
闲话不多说,项目中发现一个应用最多的功能就是选择时间,然这个东西所有用到的页面都是重复的代码,作为一个懒到极致的程序员,看到就觉得累,果断封装,先看下效果图

屏幕快照 2017-08-24 上午11.23.08.png

使用方法

//默认选中最后一个
    CWCUtilitiChooseTimeView *timeView = [[CWCUtilitiChooseTimeView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-62, [UIScreen mainScreen].bounds.size.width, 62) timeArray:[@[@"2017-01",@"2017-02",@"2017-03",@"2017-04",@"2017-05",@"2017-06",@"2017-07",@"2017-08"] mutableCopy]];
    
    __weak __typeof(&*self)ws = self;
    
    timeView.didSelectTimeTitle=^(NSString *title){
    
        //此处处理选择时间后的操作
        
        UILabel *label = (UILabel *)[ws.view viewWithTag:824];
        
        label.text = [NSString stringWithFormat:@"您选择的时间是:%@",title];
    };
    
    [self.view addSubview:timeView];

详细说明

1.由于选择的时间需要居中,所以时间数组需要添加两个空字符占位,以保证有时间显示的项可以居中

self = [super initWithFrame:frame];
    if (self) {
       
        [timeArray addObject:@"累计"];
        [timeArray addObject:@""];
        [timeArray insertObject:@"" atIndex:0];
        
        _timeDataArray = timeArray;
        self.backgroundColor = [UIColor clearColor];
        [self useTimeData];
        
    }

2、由于可选择的刷新点不会太多,本demo没有做复用优化,所有的视图都没有复用

for (int i = 0; i < self.timeDataArray.count; i++)
    {
        if (i == 0) {
            continue;
        }
        if (i == self.timeDataArray.count - 1) {
            continue;
        }
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(i*width, 0, width, 40)];
        
        [button setTitle:[self.timeDataArray objectAtIndex:i] forState:normal];
        
        
        
            if (i == self.timeDataArray.count - 2)
            {
                [button setTintColor:[UIColor whiteColor]];
            }
            else
            {
                [button setTitleColor:[self colorWithHexString:@"#235aba"] forState:normal];
            }
    
        button.backgroundColor = [UIColor clearColor];
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        [button addTarget:self action:@selector(dateBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = i+1000;
        [self.myScrollView addSubview:button];
        
    }

3、滑动时间视图的处理,滑动结束的毁掉采用block,清晰易懂

CGFloat width = (SCREENWIDTH-20)/3;
    
    float contentOffset_X = roundf(scrollView.contentOffset.x / width);
    [scrollView setContentOffset:CGPointMake(contentOffset_X*width, 0) animated:NO];
    
    CGFloat getDateNumInt;
    
    getDateNumInt = contentOffset_X;
    NSInteger TAG = 1001;
        
        NSString *blockTitle = @"all";
        if ([[self.timeDataArray objectAtIndex:getDateNumInt+1] isEqualToString:@"累计"])
        {
            
        }
        else
        {
            blockTitle =[self.timeDataArray objectAtIndex:getDateNumInt+1];
            
        }
        
        if (self.didSelectTimeTitle) {
            self.didSelectTimeTitle(blockTitle);
        }
        
        for (id objc in self.myScrollView.subviews)
        {
            if ([objc isKindOfClass:[UIButton class]]) {
                UIButton *tempbutton = objc;
                [tempbutton setTitleColor:[self colorWithHexString:@"#235aba"] forState:normal];
            }
        }
        UIButton *button = (UIButton *)[self viewWithTag:getDateNumInt+TAG];
        [button setTitleColor:[UIColor whiteColor] forState:normal];

4、问题,在滑动的回调方法中要同事实现

//滑动减速结束
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
//拖动结束,没有减速过程
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;

以上两个方法都需实现,否则您会发现滑动结束或是多动结束会有一种情况没有反应

以上的控件比较轻量级,没有其他复杂的功能,至于底色、字体、一行显示时间数等,可自行修改,下面是效果

IMG_2900.GIF

demo地址https://github.com/wenchang1989/CWCChooseRefreshTeme

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

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,446评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,250评论 4 61
  • 留给诗歌的就这个小小的一隅 还有发酵的情绪 欲说还休的夜晚 加点孤独矫情 单曲循环算不算? 感觉恐惧了...
    顾姜生阅读 157评论 0 0
  • 这两幅照片给兜兜一看,他发出来爆笑如雷的声音,迟迟不能停止(o^^o)…… 有这么搞笑嘛(^_^) 我猜因为他回想...
    稚言之语阅读 213评论 0 2
  • 经历了许多事,经历了许多人,走着他人难以理解的人生道路,未曾质疑过。 渴望太多,却不懂得如何学会知足。 时间是一块...
    鲤鱼666阅读 229评论 0 0