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