如何自定义PickerView 以及改变字体大小和字体颜色



首先,我们要向系统给我们的PickerView一样,定义它的代理方法和数据源方法。

#import <UIKit/UIKit.h>


@class MyPickerView;

//数据源方法

@protocol UIMyPickerDataSource <NSObject>

@required

- (NSInteger)numberOfComponentsInMyPickerView:(nonnull MyPickerView *)myPickerView;//包含列数

@required

- (NSInteger)myPickerView:(nonnullMyPickerView *)myPickerView numberOfRowInComponent:(NSInteger)component;//某一列的行数

@required

- (nullable NSString *)myPickerView:(nonnullMyPickerView *)myPickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

@end


//代理方法

@protocol UIMyPickerDelegate <NSObject>

- (void)myPickerView:(nonnullMyPickerView *)myPickerView titleForDidSelectRow:(NSInteger)row forComponent:(NSInteger)component;

@end


@interface MyPickerView : UIView

@property (nonatomic,strong)NSMutableArray *_Nonnull ScrolleViewArray;

@property (nonatomic,assign)_Nonnull id <UIMyPickerDataSource> dataSource;

@property (nonatomic,assign)_Nonnull id <UIMyPickerDelegate> delegate;

@end


然后我们来理一下思路。

其次我写的这个是有五行,三列,每列用了5个scrollview。


#import "MyPickerView.h"


@interface MyPickerView()<UIScrollViewDelegate>

@end


@implementation MyPickerView{

    NSArray *_dataArray;

    NSInteger _components;

    CGFloat _height;

    CGFloat _width;

    CGFloat _componentWidth;

    CGFloat _rowHeight;

    NSInteger _flagRow;

    NSInteger _lastFlagRow;

    CGFloat _ScrollRowHeight;

}

/*

 // Only override drawRect: if you perform custom drawing.

 // An empty implementation adversely affects performance during animation.

 - (void)drawRect:(CGRect)rect {

 // Drawing code

 }

 */

- (instancetype)initWithFrame:(CGRect)frame{

    self = [superinitWithFrame:frame];

    if (self) {

        _ScrolleViewArray = [[NSMutableArrayalloc]init];

        _flagRow = 0;

        _lastFlagRow = 0;

        self.userInteractionEnabled =YES;

        self.clipsToBounds =YES;

        _height = 140;

        _ScrollRowHeight =_height/5.00;

        _width = frame.size.width-60;

        self.backgroundColor = [UIColorwhiteColor];

    }

    return self;

}


-(void)setDataSource:(id<UIMyPickerDataSource>)dataSource{

    

    _dataArray =@[@"",@"",@""];

    _dataSource = dataSource;

    _components = [_dataSourcenumberOfComponentsInMyPickerView:self];

    _componentWidth = ((_width-_components-1)*100)/(_components*100.00);

    

    UIView *leftView= [[UIViewalloc]initWithFrame:CGRectMake(0,55, 40, 40)];

    leftView.backgroundColor = [UIColorcolorWithRed:191/255.0green:215/255.0blue:242/255.0alpha:1];

    leftView.layer.cornerRadius =10;

    [self addSubview:leftView];

    for (NSInteger i =0; i<_components+1; i++) {

        UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(30+i*(_componentWidth+1),10, 1, _height)];

        label.backgroundColor = [UIColorcolorWithRed:127/255.0green:127/255.0blue:127/255.0alpha:0.8];

        [self addSubview:label];

        

        if (i!=_components) {

            NSInteger _rows = [_dataSourcemyPickerView:selfnumberOfRowInComponent:i];

            //NSLog(@"_rows--%ld",_rows);

            UIScrollView *bottomScrollView1 = [[UIScrollViewalloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame),5, _componentWidth,20)];

            //bottomScrollView1.delegate = self;

            bottomScrollView1.tag = 501+i*10;

            bottomScrollView1.decelerationRate =0.1;

            bottomScrollView1.showsHorizontalScrollIndicator =NO;

            bottomScrollView1.showsVerticalScrollIndicator =NO;

            bottomScrollView1.bounces = NO;

            bottomScrollView1.alwaysBounceHorizontal =NO;

            bottomScrollView1.alwaysBounceHorizontal =NO;

            bottomScrollView1.userInteractionEnabled =NO;

            

            bottomScrollView1.backgroundColor = [UIColorwhiteColor];

            

            bottomScrollView1.contentSize = CGSizeMake(_componentWidth, 20*_rows);

            bottomScrollView1.contentOffset = CGPointMake(0, -40);

            

            [self addSubview:bottomScrollView1];

            

            for (NSInteger j =0; j<_rows; j++) {

                UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,20*j, _componentWidth,20)];

                [bottomScrollView1 addSubview:label];

                label.tag = 600+(100*i)+j;

                label.alpha = 0.3;

                label.font = [UIFontsystemFontOfSize:16];

                label.textColor = [UIColorcolorWithRed:150/255.0green:150/255.0blue:150/255.0alpha:1];

                label.textAlignment = NSTextAlignmentCenter;

                label.text = [_dataSourcemyPickerView:selftitleForRow:j forComponent:i];

                label.backgroundColor = [UIColorwhiteColor];

            }

            

            UIScrollView *middleScrollView1 =[[UIScrollView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame),CGRectGetMaxY(bottomScrollView1.frame),_componentWidth, 30)];

            //middleScrollView1.delegate = self;

            middleScrollView1.tag = 502+i*10;

            middleScrollView1.decelerationRate =0.1;

            middleScrollView1.showsHorizontalScrollIndicator =NO;

            middleScrollView1.showsVerticalScrollIndicator =NO;

            middleScrollView1.bounces = NO;

            middleScrollView1.alwaysBounceHorizontal =NO;

            middleScrollView1.alwaysBounceHorizontal =NO;

            middleScrollView1.userInteractionEnabled =NO;

            

            middleScrollView1.backgroundColor = [UIColorwhiteColor];

            

            middleScrollView1.contentSize = CGSizeMake(_componentWidth, 30*(_rows));

            middleScrollView1.contentOffset = CGPointMake(0, -30);

            

            [self addSubview:middleScrollView1];

            for (NSInteger j =0; j<_rows; j++) {

                UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,30*j, _componentWidth,30)];

                [middleScrollView1 addSubview:label];

                label.tag = 600+(100*i)+j+20;

                label.alpha = 0.5;

                label.textColor = [UIColorcolorWithRed:150/255.0green:150/255.0blue:150/255.0alpha:1];

                label.font = [UIFontsystemFontOfSize:25];

                label.textAlignment = NSTextAlignmentCenter;

                label.text = [_dataSourcemyPickerView:selftitleForRow:j forComponent:i];

                label.backgroundColor = [UIColorwhiteColor];

            }

            

            

            

            UIScrollView *topScrollView =[[ UIScrollView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame),55, _componentWidth,40)];

            //topScrollView.delegate = self;

            topScrollView.tag = 503+i*10;

            topScrollView.backgroundColor = [UIColorcolorWithRed:191/255.0green:215/255.0blue:242/255.0alpha:1];

            

            

            topScrollView.decelerationRate = 0.1;

            topScrollView.showsHorizontalScrollIndicator =NO;

            topScrollView.showsVerticalScrollIndicator =NO;

            topScrollView.bounces = NO;

            topScrollView.userInteractionEnabled =NO;

            topScrollView.alwaysBounceHorizontal =NO;

            topScrollView.alwaysBounceHorizontal =NO;

            topScrollView.contentOffset = CGPointMake(0, 0);

            topScrollView.contentSize = CGSizeMake(_componentWidth, 40*_rows);

            [self addSubview:topScrollView];

            for (NSInteger j =0; j<_rows; j++) {

                //NSLog(@"j==%ld",j);

                UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,40*j, _componentWidth,40)];

                [topScrollView addSubview:label];

                label.tag = 600+(100*i)+j+40;

                label.textColor = [UIColorcolorWithRed:36/255.0green:119/255.0blue:212/255.0alpha:1];

                label.font = [UIFontsystemFontOfSize:34];

                label.backgroundColor = [UIColorcolorWithRed:191/255.0green:215/255.0blue:242/255.0alpha:0];

                label.textAlignment = NSTextAlignmentCenter;

                label.text = [_dataSourcemyPickerView:selftitleForRow:jforComponent:i];//[NSString stringWithFormat:@"%ld",label.tag];//

            }

            

            UILabel *unitLabel = [[UILabelalloc]initWithFrame:CGRectMake(CGRectGetMaxX(topScrollView.frame)-25,CGRectGetMaxY(topScrollView.frame)-20,20, 20)];

            [self addSubview:unitLabel];

            unitLabel.text = _dataArray[i];

            //unitLabel.backgroundColor = [UIColor whiteColor];

            unitLabel.textColor = [UIColorcolorWithRed:36/255.0green:119/255.0blue:212/255.0alpha:1];

            unitLabel.font = [UIFontboldSystemFontOfSize:16];

            

            UIScrollView *middleScrollView2 =[[UIScrollView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame),CGRectGetMaxY(topScrollView.frame),_componentWidth, 30)];

            //middleScrollView2.delegate = self;

            middleScrollView2.tag = 504+i*10;

            middleScrollView2.decelerationRate =0.1;

            middleScrollView2.showsHorizontalScrollIndicator =NO;

            middleScrollView2.showsVerticalScrollIndicator =NO;

            middleScrollView2.bounces = NO;

            middleScrollView2.alwaysBounceHorizontal =NO;

            middleScrollView2.alwaysBounceHorizontal =NO;

            middleScrollView2.userInteractionEnabled =NO;

            

            middleScrollView2.backgroundColor = [UIColorwhiteColor];

            

            middleScrollView2.contentOffset = CGPointMake(0, 30);

            [self addSubview:middleScrollView2];

            middleScrollView2.contentSize = CGSizeMake(_componentWidth, 30*(_rows));

            for (NSInteger j =0; j<_rows; j++) {

                UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,30*j, _componentWidth,30)];

                [middleScrollView2 addSubview:label];

                label.tag = 600+(100*i)+j+60;

                label.alpha = 0.5;

                label.font = [UIFontsystemFontOfSize:25];

                label.textColor = [UIColorcolorWithRed:150/255.0green:150/255.0blue:150/255.0alpha:1];

                label.textAlignment = NSTextAlignmentCenter;

                label.text = [_dataSourcemyPickerView:selftitleForRow:j forComponent:i];

                label.backgroundColor = [UIColorwhiteColor];

            }

            

            

            //27376

            

            UIScrollView *bottomScrollView2 = [[UIScrollViewalloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame),CGRectGetMaxY(middleScrollView2.frame),_componentWidth, 20)];

            //bottomScrollView2.delegate = self;

            bottomScrollView2.tag = 505+i*10;

            bottomScrollView2.decelerationRate =0.1;

            bottomScrollView2.showsHorizontalScrollIndicator =NO;

            bottomScrollView2.showsVerticalScrollIndicator =NO;

            bottomScrollView2.bounces = NO;

            bottomScrollView2.alwaysBounceHorizontal =NO;

            bottomScrollView2.alwaysBounceHorizontal =NO;

            bottomScrollView2.userInteractionEnabled =NO;

            

            bottomScrollView2.backgroundColor = [UIColorwhiteColor];

            

            bottomScrollView2.contentOffset = CGPointMake(0, 40);

            bottomScrollView2.contentSize = CGSizeMake(_componentWidth, 20*_rows);

            [self addSubview:bottomScrollView2];

            for (NSInteger j =0; j<_rows; j++) {

                UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,20*j, _componentWidth,20)];

                [bottomScrollView2 addSubview:label];

                label.tag = 600+(100*i)+j+80;

                label.alpha = 0.3;

                label.textColor = [UIColorcolorWithRed:150/255.0green:150/255.0blue:150/255.0alpha:1];

                label.textAlignment = NSTextAlignmentCenter;

                label.font = [UIFontsystemFontOfSize:16];

                label.text = [_dataSourcemyPickerView:selftitleForRow:j forComponent:i];

                label.backgroundColor = [UIColorwhiteColor];

            }

            

            UIScrollView *_scrollView= [[UIScrollViewalloc]initWithFrame:CGRectMake(CGRectGetMaxX(label.frame),0, _componentWidth,_height)];

            _scrollView.userInteractionEnabled =YES;

            [self addSubview:_scrollView];

            _scrollView.tag = 506+i*10;

            

            _scrollView.decelerationRate = 0.01;

            //_scrollView.showsHorizontalScrollIndicator = NO;

            _scrollView.showsVerticalScrollIndicator =NO;

            _scrollView.bounces = NO;

            _scrollView.contentSize = CGSizeMake(_componentWidth, 40*(_rows+2.5));

            

            _scrollView.contentOffset = CGPointMake(0, 0);

            _scrollView.delegate = self;


            

            NSArray *array = @[bottomScrollView1,middleScrollView1,topScrollView,middleScrollView2,bottomScrollView2];

            [_ScrolleViewArray addObject:array];

            

        }

        if(i==_components-1){

            UIView *rightView= [[UIViewalloc]initWithFrame:CGRectMake(_width+20,55, 40, 40)];

            rightView.backgroundColor = [UIColorcolorWithRed:191/255.0green:215/255.0blue:242/255.0alpha:1];

            rightView.layer.cornerRadius =10;

            [self addSubview:rightView];

        }

        

    }

}

- (void)setDelegate:(id<UIMyPickerDelegate>)delegate{

    _delegate = delegate;

}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    CGFloat offSet = scrollView.contentOffset.y;

    CGFloat _offset = offSet/40.00;

    _flagRow = (NSInteger)(offSet/40.00+0.5);

    if (_flagRow!=_lastFlagRow) {

        _lastFlagRow =_flagRow;

       // NSLog(@"_flagRow%ld",_flagRow);

    }

    //NSLog(@"tag_%ld",scrollView.tag);

    

    NSInteger flag = scrollView.tag-500;

    NSInteger componentFlag = flag/10;

    NSArray *array = (NSArray *)_ScrolleViewArray[componentFlag];

    for (NSInteger i=0; i<5; i++) {

        UIScrollView *scrollView = (UIScrollView *)array[i];


        if (i==0){

            scrollView.contentOffset = CGPointMake(0, 20*_offset-40);

        }

        if(i==1) {

            scrollView.contentOffset = CGPointMake(0, 30*_offset-30);

        }

        if (i==2){

            scrollView.contentOffset = CGPointMake(0, offSet);

        }

        if(i==3) {

            scrollView.contentOffset = CGPointMake(0, 30*_offset+30);

        }

        if(i==4){

            scrollView.contentOffset = CGPointMake(0, 20*_offset+40);

        }

    }

}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

    NSLog(@"停止拖拽");

    [self setUpDelegateWith:scrollView];

}


- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{

    NSLog(@"开始减速");

}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    

    NSLog(@"减速结束");

    [self setUpDelegateWith:scrollView];

    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

    //        if (_delegate && [_delegate respondsToSelector:@selector(myPickerView:titleForDidSelectRow:forComponent:)]) {

    //            [_delegate myPickerView:self titleForDidSelectRow:_offSetY forComponent:componentFlag];

    //        }

    //    });

}


-(void)setUpDelegateWith:(UIScrollView *)scrollView{

    CGFloat offSetY = scrollView.contentOffset.y;

    NSInteger _offSetY = offSetY/40.00+0.5;

    NSInteger flag = scrollView.tag-500;

    NSInteger componentFlag = flag/10;

    NSArray *array = (NSArray *)_ScrolleViewArray[componentFlag];

    for (NSInteger i=0; i<5; i++) {

        UIScrollView *scrollView = (UIScrollView *)array[i];

        

        if (i==0){//NSLog(@"%ld",scrollView.tag);

            [scrollView setContentOffset:CGPointMake(0,20*_offSetY-40)animated:YES];

        }

        if(i==1) {//NSLog(@"%ld",scrollView.tag);

            [scrollView setContentOffset:CGPointMake(0,30*_offSetY-30)animated:YES];

        }

        if (i==2){//NSLog(@"%ld",scrollView.tag);

            [scrollView setContentOffset:CGPointMake(0,40*_offSetY) animated:YES];

        }

        if(i==3) {//NSLog(@"%ld",scrollView.tag);

            [scrollView setContentOffset:CGPointMake(0,30*_offSetY+30)animated:YES];

        }

        if(i==4){//NSLog(@"%ld",scrollView.tag);

            [scrollView setContentOffset:CGPointMake(0,20*_offSetY+40)animated:YES];

        }

    }

    if (_delegate && [_delegaterespondsToSelector:@selector(myPickerView:titleForDidSelectRow:forComponent:)]) {

        [_delegatemyPickerView:selftitleForDidSelectRow:_offSetY forComponent:componentFlag];}

}


- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{

    NSLog(@"滑动动画结束");

}

@end


代码写的比较啰嗦,但是一般人都能看懂,有人需要就用一下吧
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值