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)為其他人也可能受益,請把它分享出去。