2017年3月13日
一.通用拨盘控件(支持多级拨盘,例子里用的是一级)
1:效果
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];//显示默认值
}
如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。