通用拨盘控件

2017年3月13日
一.通用拨盘控件(支持多级拨盘,例子里用的是一级)
1:效果


Paste_Image.png

2.实现:

//自定义控件相关宏定义
  //通用拨盘控件
#define pickerViewSheet_titleBar_height 44
#define pickerViewSheet_contentView_height 216
#define pickerViewSheet_height (pickerViewSheet_titleBar_height+pickerViewSheet_contentView_height)

//  HuPickerViewSheet.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@protocol HuPickerViewSheetDelegate <NSObject>

@optional

- (void)onSheetDidDone;
- (void)onSheetDidCancel;

- (void)OnMultiSheetDidDone:(UIView *)view;
- (void)OnMultiSheetDidCancel:(UIView *)view;

@end

@interface HuPickerViewSheet : UIView
{
    UIToolbar          *_titleBar;
    UIBarButtonItem    *_title;
    UIBarButtonItem    *_leftButton;  //默认是  取消
    UIBarButtonItem    *_rightButton;  //   确定

    UIView              *_currentView;
@private
    UIControl          *_backgroundView;
}

@property(nonatomic, assign) id<HuPickerViewSheetDelegate> sheetDelegate;

- (id)initWithContentView:(UIView *)contentView;

- (void)setSheetTitle:(NSString*)title;

- (void)setCancelButtonTitle:(NSString*)title;

- (void)setDoneButtonTitle:(NSString*)title;

- (void)show; //显示拨盘

- (void)disMissView;

- (void)doCancel;

+ (CGFloat)height;

@end

//  HuPickerViewSheet.m
#import "HuPickerViewSheet.h"

@implementation HuPickerViewSheet

- (void)dealloc
{
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithContentView:(UIView *)contentView{

    self = [super init];
    if (self)
    {
        self.backgroundColor = [UIColor whiteColor];
        self.frame = CGRectMake(0, 0, HHBWIDTH, pickerViewSheet_height);

        CGFloat yPos = pickerViewSheet_titleBar_height;
        CGFloat height = pickerViewSheet_contentView_height;
        CGFloat width = HHBWIDTH;
        contentView.frame = CGRectMake(0, yPos, width, height); //PickView 纵向固定大小:320x216 横向固定大小:480x162
        _currentView = contentView;

        height = pickerViewSheet_titleBar_height;
        _titleBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, width, height)];
        _titleBar.barStyle = UIBarStyleDefault;
        [_titleBar setBarTintColor:[UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1.0]];

        _title = [[UIBarButtonItem alloc] initWithTitle:nil style: UIBarButtonItemStylePlain target: nil action: nil];

        width = height = pickerViewSheet_titleBar_height;
        UIButton *tempBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, height)];
        [tempBtn setTitleColor:[UIColor colorWithRed:0.09 green:0.47 blue:0.94 alpha:1.0] forState:UIControlStateNormal];
        [tempBtn setTitle:@"取消" forState:UIControlStateNormal];
        [tempBtn addTarget:self action:@selector(doCancel) forControlEvents:UIControlEventTouchUpInside];
        _leftButton =  [[UIBarButtonItem alloc] initWithCustomView:tempBtn];

        UIButton *tempBtnConfirm = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, height)];
        [tempBtnConfirm setTitleColor:[UIColor colorWithRed:0.09 green:0.47 blue:0.94 alpha:1.0] forState:UIControlStateNormal];
        [tempBtnConfirm setTitle:@"确定" forState:UIControlStateNormal];
        [tempBtnConfirm addTarget:self action:@selector(doOK) forControlEvents:UIControlEventTouchUpInside];
        _rightButton =  [[UIBarButtonItem alloc] initWithCustomView:tempBtnConfirm];

        UIBarButtonItem *fixedButton  = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target: nil action: nil];

        NSArray *array = [[NSArray alloc] initWithObjects: _leftButton,fixedButton, _title,fixedButton, _rightButton, nil];
        [_titleBar setItems: array];

        [self addSubview:_titleBar];
        [self addSubview:contentView];

    }
    return self;
}

- (void)setSheetTitle:(NSString*)title{

    UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, HHBWIDTH/2.0, pickerViewSheet_titleBar_height)];
    label.text = title;
    label.font = [UIFont systemFontOfSize:18.0];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor blackColor];

    _title.customView = label;
}

- (void)setCancelButtonTitle:(NSString*)title{
    [_leftButton setTitle:title];
}

- (void)setDoneButtonTitle:(NSString*)title{
    [_rightButton setTitle:title];
}

- (void)doOK{
    if (_sheetDelegate && [_sheetDelegate respondsToSelector:@selector(onSheetDidDone)]) {
        [_sheetDelegate onSheetDidDone];
    }
    else if (_sheetDelegate && [_sheetDelegate respondsToSelector:@selector(OnMultiSheetDidDone:)]) {
        [_sheetDelegate OnMultiSheetDidDone:_currentView];
    }

    [self dismissViews];
}

-(void)doCancel{
    if (_sheetDelegate && [_sheetDelegate respondsToSelector:@selector(onSheetDidCancel)]) {
        [_sheetDelegate onSheetDidCancel];
    }
    else if (_sheetDelegate && [_sheetDelegate respondsToSelector:@selector(OnMultiSheetDidCancel:)]) {
        [_sheetDelegate OnMultiSheetDidCancel:_currentView];
    }

    [self dismissViews];
}

- (void)dismissViews
{
    CGRect tempFrame = self.frame;
    tempFrame.origin.y = [UIScreen mainScreen].bounds.size.height;

    [UIView animateWithDuration:0.3f
                     animations:^{
                         self.frame = tempFrame;
                     }
                     completion:^(BOOL finished) {
                         [self removeFromSuperview];
                         [_backgroundView removeFromSuperview];
                     }];
}

- (void)show
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;

    //begin 为偶现的卡死而做的优化
    [window setFrame:[[UIScreen mainScreen] bounds]];
    [window makeKeyWindow];
    //end 为偶现的卡死而做的优化

    if (window.frame.size.height > self.frame.size.height) {
        if (!_backgroundView) {

            _backgroundView = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, window.frame.size.width, window.frame.size.height)];

            _backgroundView.backgroundColor = [UIColor lightGrayColor];

            _backgroundView.alpha = 0.4;

            [_backgroundView addTarget:self action:@selector(doCancel) forControlEvents:UIControlEventTouchDown];
        }
        [window addSubview:_backgroundView];

        CGRect tempFrame = self.frame;
        CGFloat destOriginY = window.frame.size.height - self.frame.size.height;

        if (tempFrame.origin.y < destOriginY)
        { //此处为了动画是从下往上弹出的
            tempFrame.origin.y = window.frame.size.height;
            self.frame = tempFrame;
        }
        tempFrame.origin.y = destOriginY;

        [window addSubview:self];

        [UIView animateWithDuration:0.3f
                         animations:^{
                             self.frame = tempFrame;
                         }
                         completion:nil];
    }
}

- (void)disMissView
{
    [self removeFromSuperview];
    [_backgroundView removeFromSuperview];
}

+ (CGFloat)height
{
    return pickerViewSheet_height;
}

@end

3.使用

3.0通用key value结构类

//  HuShareItems.h

#import <UIKit/UIKit.h>

@interface HuKeyTitleObject : NSObject {
    NSString *_keyString;
    NSString *_titleString;
}
@property (nonatomic, strong) NSString *key;
@property (nonatomic, strong) NSString *title;

+ (id)keyTitleWithKey:(NSString *)key andTitile:(NSString *)title;

@end

//  HuShareItems.m
#import "HuShareItems.h"
@implementation HuKeyTitleObject

@synthesize key = _keyString;
@synthesize title = _titleString;

+ (id)keyTitleWithKey:(NSString *)key andTitile:(NSString *)title
{
    HuKeyTitleObject *object = [[HuKeyTitleObject alloc] init];
    object.key = key;
    object.title = title;

    return object;
}

@end

3.1定义相关成员变量(如果一个界面里有多个拨盘数据,只要数组和索引定义多个对应结构即可

@interface HuTestResultViewController ()<UIPickerViewDataSource, UIPickerViewDelegate,HuPickerViewSheetDelegate>
{
    HuPickerViewSheet  *_pickerSheet; //拨盘选择器
    UIPickerView      *_pickerView; //
    NSMutableArray    *_kindArray;//拨盘种类数组
    NSInteger          _kindIndex;//当前下标索引
}

3.2初始化

#define KAllExercise   @"全部题目"
#define kRightExercise @"只看正确题目"
#define kWrongExercise @"只看错误题目"

- (void)initKindArray
{
    if (_kindArray == nil) {
        _kindArray = [NSMutableArray arrayWithCapacity:3];
    }
    if ([_kindArray count] == 0) {
        NSString *kindStr = [NSString stringWithFormat:@"%ld:%@,%ld:%@,%ld:%@",HuTestResultShowExercisesTypeAll,KAllExercise,HuTestResultShowExercisesTypeOnlyRight,kRightExercise,HuTestResultShowExercisesTypeOnlyWrong,kWrongExercise];
        NSArray *tempArr = [kindStr componentsSeparatedByString:@","];
        for(NSString *fstr in tempArr)
        {
            NSArray *arr = [fstr componentsSeparatedByString:@":"];
            [_kindArray addObject:[HuKeyTitleObject keyTitleWithKey:[arr objectAtIndex:0] andTitile:[arr objectAtIndex:1]]];
        }
    }
    [_pickerView reloadComponent: 0];
    //默认值 默认显示全部题目
    _kindIndex = _showExercisesType;
    HuKeyTitleObject *object = [_kindArray objectAtIndex: _kindIndex];
    _filterL.text = object.title;
}

3.3 实现delegate相关动作方法

#pragma mark - pickerview delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [_kindArray count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if(row < [_kindArray count])
    {
        HuKeyTitleObject *keyTitle = [_kindArray objectAtIndex: row];
        return keyTitle.title;
    }

    return nil;
}

- (void)onSheetDidDone
{
    NSInteger selected = [_pickerView selectedRowInComponent: 0];
    if (selected > [_kindArray count] - 1) {
        selected = 0;
    }
    _kindIndex = selected;
    HuKeyTitleObject *object = [_kindArray objectAtIndex: _kindIndex];
    _filterL.text = object.title;
//重新过滤tableview
    _showExercisesType = [object.key integerValue];
    [_tableView reloadData];
}

- (void)onSheetDidCancel
{
}

3.4点击按钮弹出拨盘添加对应方法

- (void)btnClick:(UIButton*)btn
{
    [self showPickerView];
}

- (void)showPickerView
{
    //如果有textField,这里就要移除焦点
    if (_pickerView == nil) {
        _pickerView = [[UIPickerView alloc] init];
        _pickerView.showsSelectionIndicator = YES;
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
    }
    if (_pickerSheet == nil) {
        _pickerSheet = [[HuPickerViewSheet alloc] initWithContentView: _pickerView];
        _pickerSheet.sheetDelegate = (id<HuPickerViewSheetDelegate>) self;
    }
    [_pickerSheet show];
    [_pickerSheet setSheetTitle: @"选择题目"];
    [_pickerView selectRow: _kindIndex inComponent: 0 animated: NO];//显示默认值
}

如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,905评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,140评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,791评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,483评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,476评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,516评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,905评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,560评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,778评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,557评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,635评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,338评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,925评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,898评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,142评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,818评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,347评论 2 342

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,082评论 1 32
  • iOS网络架构讨论梳理整理中。。。 其实如果没有APIManager这一层是没法使用delegate的,毕竟多个单...
    yhtang阅读 5,160评论 1 23
  • 这是16年5月份编辑的一份比较杂乱适合自己观看的学习记录文档,今天18年5月份再次想写文章,发现简书还为我保存起的...
    Jenaral阅读 2,727评论 2 9
  • 关于教育问题,一直是有些疑惑的。 看到的遇到的人,都极有礼貌,客气而职业的微笑。无论是上了年纪的老爷爷老奶奶,还是...
    归人无梦阅读 206评论 0 2
  • 雄姿铁蹄闪金瞳,骧首扬鬃见威风。 豪迈奔放禀龙性,骏骨骙骙万夫勇。 骞蹄忘我如电掣,朝天一嘶贯长虹。 不惧千难与万...
    欣荣Y阅读 461评论 14 29