iOS自定义无限循环选择器

主要思路:
1、首先引入头文件
2、画UI
3、准备数据源
4、实现代理方法

1、首先引入头文件

#import "CLZoomPickerView.h"

两个代理<CLZoomPickerViewDelegate,CLZoomPickerViewDataSource>

2、画UI

@property(nonatomic,strong)UIImgView *backImageView;

@property(nonatomic,strong)CLZoomPickerView *leftPickerView;

@property(nonatomic,strong)CLZoomPickerView *rightPickerView;

@property(nonatomic,strong)NSMutableArray *leftDataSource;

@property(nonatomic,strong)NSMutableArray *rightDataSource;
#pragma mark  初始化backImageView

_backImageView = [[UIImageView alloc]init];

_backImageView.frame = CGRectMake(0, 0,UI_View_Width,400);

_backImageView.image = [UIImage imageNamed:@"首页背景"];

_backImageView.userInteractionEnabled = YES;

[cell.contentView addSubview:_backImageView];
#pragma mark  初始化PickerView

- (void)initLeftPickerView{

_leftPickerView = [[CLZoomPickerView alloc]init];

_leftPickerView.frame = CGRectMake(0, -40, _backImageView.width/2.0, _backImageView.height+80);

_leftPickerView.dataSource = self;

_leftPickerView.delegate = self;

_leftPickerView.topRowCount = 2;

_leftPickerView.bottomRowCount = 2;

_leftPickerView.selectedRow = 2;

_leftPickerView.rowHeight = _leftPickerView.height/7.0;

_leftPickerView.selectedRowFont = [UIFont systemFontOfSize:35];

_leftPickerView.textColor = [UIColor whiteColor];

_leftPickerView.unselectedRowScale = 0.5;

[_backImageView addSubview:_leftPickerView];

UILabel *yuanLabel = [[UILabel alloc]init];

yuanLabel.frame = CGRectMake(UI_View_Width/2.0-43, _leftPickerView.height/2.0, 15, 15);

yuanLabel.font = [UIFont systemFontOfSize:15];

yuanLabel.textColor = [UIColor whiteColor];

yuanLabel.text = @"元";

[_leftPickerView addSubview:yuanLabel];

}
- (void)initRightPickerView{

_rightPickerView = [[CLZoomPickerView alloc]init];

_rightPickerView.frame = CGRectMake(_backImageView.width/2.0, -40, _backImageView.width/2.0, _backImageView.height+80);

_rightPickerView.dataSource = self;

_rightPickerView.delegate = self;

_rightPickerView.topRowCount = 2;

_rightPickerView.bottomRowCount = 2;

_rightPickerView.selectedRow = 2;

_rightPickerView.rowHeight = _rightPickerView.height/7.0;

_rightPickerView.selectedRowFont = [UIFont systemFontOfSize:35];

_rightPickerView.textColor = [UIColor whiteColor];

_rightPickerView.unselectedRowScale = 0.5;

[_backImageView addSubview:_rightPickerView];

UILabel *tianLabel = [[UILabel alloc]init];

tianLabel.frame = CGRectMake(UI_View_Width/2.0-45-18, _leftPickerView.height/2.0, 15, 15);

tianLabel.font = [UIFont systemFontOfSize:15];

tianLabel.textColor = [UIColor whiteColor];

tianLabel.text = @"天";

[_rightPickerView addSubview:tianLabel];

}

3、准备数据源

NSMutableArray *leftArray = [[NSMutableArray alloc]init];

for (int i=5; i<=50; i++) {

[leftArray addObject:[NSString stringWithFormat:@"%d",i*100]];

}

_leftDataSource = [[NSMutableArray alloc]init];

//数据源需要添加三次实现无缝隙连接

[_leftDataSource addObjectsFromArray:leftArray];

[_leftDataSource addObjectsFromArray:leftArray];

[_leftDataSource addObjectsFromArray:leftArray];

[self initLeftPickerView];

NSMutableArray *rightArray = [[NSMutableArray alloc]init];

if ([HemaFunction xfuncGetAppdelegate].isLogin) {

for (int i=7; i<=30; i++) {

[rightArray addObject:[NSString stringWithFormat:@"%d",i]];

}

_rightDataSource = [[NSMutableArray alloc]init];

[_rightDataSource addObjectsFromArray:rightArray];

[_rightDataSource addObjectsFromArray:rightArray];

[_rightDataSource addObjectsFromArray:rightArray];

[self initRightPickerView];

4、实现代理方法

@property(nonatomic,copy)NSString *loanmoney;

@property(nonatomic,copy)NSString *loanday;
#pragma mark - CLZoomPickerView 代理

// CLZoomPickerView 代理,当前项改变后调用此方法

- (void)pickerView:(CLZoomPickerView *)pickerView changedIndex:(NSUInteger)indexPath

{

     if (pickerView==_leftPickerView) {

          UILabel *label = (UILabel *)[self.view viewWithTag:99];

          label.text = [NSString stringWithFormat:@"¥%@",_leftDataSource[indexPath]];

          _loanmoney = _leftDataSource[indexPath];

    }

     if (pickerView==_rightPickerView) {

      _loanday = _rightDataSource[indexPath];

     }

}
// CLZoomPickerView 代理,返回数据行数

- (NSInteger)pickerView:(CLZoomPickerView *)pickerView

{

    if (pickerView==_leftPickerView) {

        return _leftDataSource.count/3;

    }else{

       return _rightDataSource.count/3;   

   } 

}
// CLZoomPickerView 代理,返回指定行显示的字符串

- (NSString *)pickerView:(CLZoomPickerView *)pickerView titleForRow:(NSUInteger)indexPath

{

     if (pickerView==_leftPickerView) {

          return _leftDataSource[indexPath];

     }else{

         return _rightDataSource[indexPath];

    }

}

测试看下效果:


支持类下载 Demo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、首先引入头文件 #import "CLZoomPickerView.h" 两个代理<CLZoomPickerV...
    大炮打小鸟阅读 4,117评论 0 3
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,752评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,224评论 19 139
  • 夜跑(一) 我开始恢复夜跑 像几年前等你的那样 汗水浸湿着衣裳 流入心口满是咸涩 抬头勉力的奔跑 对着泪水撒谎 我...
    米蓝色的天空阅读 2,372评论 0 6
  • “世间有多少好朋友,年龄相仿,志趣相投,原本可以一辈子莫逆相交,可谁会料到旦夕惊变,从此以后,只能眼睁睁的,看着天...
    游离_阅读 1,373评论 0 0

友情链接更多精彩内容