封装一个UIPickerView

简介: 封装一个UIPickerView

封装一个UIPickerView


先给大家看一下效果图

image.png

pickerView.gif

第一步:

image.png

第二步:

56966f9917855d643b0b33332fb7b518.png

Snip20170417_1.png

第三步:在.h文件中

Snip20170417_2.png


#import <UIKit/UIKit.h>
@protocol LYWPickerViewDelegate <NSObject>
@optional
- (void)pickViewSureBtnClick:(NSString *)selectRow;
@end
@interface LYWPickerView : UIView
/*** 提供出一个数组 方便外面传递 ***/
@property (strong, nonatomic) NSMutableArray *NumberArr;
@property (weak, nonatomic)id<LYWPickerViewDelegate> delegate;
@end
第四步:在.m文件中代码实现


#import "LYWPickerView.h"
/*** 屏幕宽 ***/
#define kScreenWidth ([UIScreen mainScreen].bounds.size.width)
/*** 屏幕高 ***/
#define kScreenHeight ([UIScreen mainScreen].bounds.size.height)
// 超级紫色
#define PURPLECOLOR [UIColor colorWithRed:155 / 255.0 green:132 / 255.0 blue:188 / 255.0 alpha:1]
#define GREENCOLOR [UIColor colorWithRed:94 / 255.0 green:224 / 255.0 blue:180 / 255.0 alpha:1]
#define BlueColor [UIColor colorWithRed:20/255.0 green:124/255.0 blue:235/255.0 alpha:1]
@interface LYWPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic, strong) UIPickerView *picker;
@property (nonatomic, strong)UIView *pickerView;
@property (nonatomic, strong)UIView *back;
@property (nonatomic, strong)NSString *title;
@property (nonatomic, strong)NSString *str;
@property (nonatomic, assign)NSInteger IdNum;
@end
@implementation LYWPickerView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self creatCaverView];
        [self creatAnimation];
    }
    return self;
}
- (void)creatAnimation
{
    [UIView animateWithDuration:0.5 animations:^{
        _back.alpha = 0.3;
        _picker.frame = CGRectMake(0, kScreenHeight - kScreenWidth / 2., kScreenWidth, kScreenWidth / 2.);
        _pickerView.frame = CGRectMake(0, kScreenHeight - kScreenWidth / 2. - 30, kScreenHeight, 30);
    }];
}
//添加遮盖层
- (void)creatCaverView
{
    //添加一个遮盖UIView
    _back = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _back.backgroundColor = [UIColor blackColor];
    _back.alpha = 0;
    [self addSubview:_back];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelPicker)];
    tap.numberOfTapsRequired = 1;
    [_back addGestureRecognizer:tap];
    [self creatPickView];
}
//picker上面的取消和确定按钮
- (void)creatPickView
{
    _pickerView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, 30)];
    _pickerView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_pickerView];
    UIView *headLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
    headLine.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:headLine];
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(10, 10, 80, 20);
    [leftButton setTitle:@"取消" forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    leftButton.titleLabel.font = [UIFont systemFontOfSize:18.];
    [_pickerView addSubview:leftButton];
    [leftButton addTarget:self action:@selector(cancelPicker) forControlEvents:UIControlEventTouchUpInside];
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(kScreenWidth - 30 - 60, 10, 80, 20);
    [rightButton setTitle:@"确定" forState:UIControlStateNormal];
    [rightButton setTitleColor:BlueColor forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:18.];
    [_pickerView addSubview:rightButton];
    [rightButton addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
    _picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, kScreenHeight + 30, kScreenWidth, kScreenWidth / 2.0)];
    _picker.delegate = self;
    _picker.dataSource = self;
    _picker.backgroundColor = [UIColor whiteColor];
    [self addSubview:_picker];
}
// picker确定按钮点击事件
- (void)confirm
{
    if (!_str) {
        _str = self.NumberArr[0];
        if([_delegate respondsToSelector:@selector(pickViewSureBtnClick:)])
            [_delegate pickViewSureBtnClick:_str];
    }
    else
    {
        if([_delegate respondsToSelector:@selector(pickViewSureBtnClick:)])
            [_delegate pickViewSureBtnClick:_str];
    }
    [self cancelPicker];
}
// 弹回picker
- (void)cancelPicker
{
    [UIView animateWithDuration:0.25 animations:^{
        _back.alpha = 0;
        _picker.frame = CGRectMake(0, kScreenHeight + 30, kScreenWidth, kScreenWidth / 2.);
        _pickerView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, 30);
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.NumberArr.count;
}
#pragma mark - UIPickerViewDelegate
// 告诉系统每一行显示什么内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [NSString stringWithFormat:@"%@",self.NumberArr[row]];
}
// 监听pickerView的选中
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _str = [self.NumberArr objectAtIndex:row];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:20]];
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}
@end

最后只需要把这个文件拖到你需要的工程就可以使用了

下面告诉大家怎么使用

Snip20170417_5.png

相关文章
|
4月前
|
人工智能 Kubernetes 数据可视化
别再写"面条式文档"了!用AI给你的思维装个"图形渲染引擎"
别让线性文字限制了你的高维思考。本文揭示了大脑作为"图形处理器"的本质,提供了一套专业的AI指令,将复杂的逻辑思维"序列化"为清晰的拓扑结构,像渲染DOM树一样可视化你的知识体系,极大提升沟通与学习效率。
440 12
|
5月前
|
JSON 缓存 API
【剪映小助手】向现有草稿中添加视频特效
向现有草稿中添加视频特效。该接口用于在指定的时间段内添加特效素材到剪映草稿中,支持多种特效类型如边框特效、滤镜特效、动态特效等。特效可以用于增强视频的视觉效果。
|
11月前
|
监控 大数据 API
Python 技术员实践指南:从项目落地到技术优化
本内容涵盖Python开发的实战项目、技术攻关与工程化实践,包括自动化脚本(日志分析系统)和Web后端(轻量化API服务)两大项目类型。通过使用正则表达式、Flask框架等技术,解决日志分析效率低与API服务性能优化等问题。同时深入探讨内存泄漏排查、CPU瓶颈优化,并提供团队协作规范与代码审查流程。延伸至AI、大数据及DevOps领域,如商品推荐系统、PySpark数据处理和Airflow任务编排,助力开发者全面提升从编码到架构的能力,积累高并发与大数据场景下的实战经验。
Python 技术员实践指南:从项目落地到技术优化
|
10月前
|
Java 定位技术 API
高德地图电话号采集工具,百度腾讯高德地图手机号采集提取工具,python版
该工具支持高德/百度/腾讯三平台商家电话采集,包含多线程处理和数据去重功能‌37。使用时
|
移动开发 JavaScript 前端开发
UniApp低代码-颜色选择器diy-color-picker-代码生成器
UniApp低代码-颜色选择器diy-color-picker-代码生成器
698 5
|
人工智能 自然语言处理 测试技术
通义灵码上新推理模型,快来体验数学编程双冠王 Qwen2.5-Max
近日,通义灵码上新模型选择功能,除新增 DeepSeek 满血版 V3 和 R1 外,Qwen2.5-Max 也正式上线,它使用了超过 20 万亿 token 的预训练数据及精心设计的后训练方案进行训练。
|
数据采集 存储 数据挖掘
TMDB电影数据分析(下)
TMDB电影数据分析(下)
1206 0
|
JavaScript 前端开发 安全
80 行 JS 代码实现页面添加水印:文字水印、多行文字水印、图片水印、文字&图片水印
80 行 JS 代码实现页面添加水印:文字水印、多行文字水印、图片水印、文字&图片水印 1. 信息标识: 水印可以用于标识文档的所有者、保密级别、状态或其他相关信息,帮助用户更好地理解文档内容的属性。 2. 版权保护: 在文档中添加水印可以帮助保护内容的版权,防止他人未经授权地复制、转载或篡改内容。 3. 安全保护: 对于敏感信息或机密文档,添加水印可以帮助防止信息泄露,提高文档的安全性。 4. 提升专业性: 在一些场景下,如商业报告、合同文件等,添加水印可以增加文档的专业性和正式性。 5. 防止截屏或拷贝: 在网页中添加水印可以防止用户通过截屏或复制粘贴等方式非法获取文档内容。
714 1
80 行 JS 代码实现页面添加水印:文字水印、多行文字水印、图片水印、文字&图片水印
|
人工智能 程序员 Python
用通义灵码创建脚本
用通义灵码创建脚本
|
前端开发 算法 计算机视觉
用canvas消除锯齿的方式
用canvas消除锯齿的方式
711 0