通用撥盤控件

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


Paste_Image.png

2.實(shí)現(xiàn):

//自定義控件相關(guān)宏定義
  //通用撥盤控件
#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;  //默認(rèn)是  取消
    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 為偶現(xiàn)的卡死而做的優(yōu)化
    [window setFrame:[[UIScreen mainScreen] bounds]];
    [window makeKeyWindow];
    //end 為偶現(xiàn)的卡死而做的優(yōu)化

    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結(jié)構(gòu)類

//  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定義相關(guān)成員變量(如果一個界面里有多個撥盤數(shù)據(jù),只要數(shù)組和索引定義多個對應(yīng)結(jié)構(gòu)即可

@interface HuTestResultViewController ()<UIPickerViewDataSource, UIPickerViewDelegate,HuPickerViewSheetDelegate>
{
    HuPickerViewSheet  *_pickerSheet; //撥盤選擇器
    UIPickerView      *_pickerView; //
    NSMutableArray    *_kindArray;//撥盤種類數(shù)組
    NSInteger          _kindIndex;//當(dāng)前下標(biāo)索引
}

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];
    //默認(rèn)值 默認(rèn)顯示全部題目
    _kindIndex = _showExercisesType;
    HuKeyTitleObject *object = [_kindArray objectAtIndex: _kindIndex];
    _filterL.text = object.title;
}

3.3 實(shí)現(xiàn)delegate相關(guān)動作方法

#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點(diǎn)擊按鈕彈出撥盤添加對應(yīng)方法

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

- (void)showPickerView
{
    //如果有textField,這里就要移除焦點(diǎn)
    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];//顯示默認(rèn)值
}

如果您發(fā)現(xiàn)本文對你有所幫助,如果您認(rèn)為其他人也可能受益,請把它分享出去。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對...
    cosWriter閱讀 11,674評論 1 32
  • iOS網(wǎng)絡(luò)架構(gòu)討論梳理整理中。。。 其實(shí)如果沒有APIManager這一層是沒法使用delegate的,畢竟多個單...
    yhtang閱讀 5,491評論 1 23
  • 這是16年5月份編輯的一份比較雜亂適合自己觀看的學(xué)習(xí)記錄文檔,今天18年5月份再次想寫文章,發(fā)現(xiàn)簡書還為我保存起的...
    Jenaral閱讀 3,146評論 2 9
  • 關(guān)于教育問題,一直是有些疑惑的。 看到的遇到的人,都極有禮貌,客氣而職業(yè)的微笑。無論是上了年紀(jì)的老爺爺老奶奶,還是...
    歸人無夢閱讀 234評論 0 2
  • 雄姿鐵蹄閃金瞳,驤首揚(yáng)鬃見威風(fēng)。 豪邁奔放稟龍性,駿骨骙骙萬夫勇。 騫蹄忘我如電掣,朝天一嘶貫長虹。 不懼千難與萬...
    欣榮Y閱讀 506評論 14 29

友情鏈接更多精彩內(nèi)容