美文网首页IOS累之用之
iOS自定义无限循环选择器

iOS自定义无限循环选择器

作者: 大炮打小鸟 | 来源:发表于2017-06-28 17:28 被阅读271次

主要思路:
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

相关文章

网友评论

    本文标题:iOS自定义无限循环选择器

    本文链接:https://www.haomeiwen.com/subject/bvnucxtx.html