键盘弹出-PickerView

原创 2016年06月02日 09:42:02

这里写图片描述

1.ViewController.m

//
//  ViewController.m
//

#import "ViewController.h"
#import "SimulateActionSheet.h"

@interface ViewController ()<SimulateActionSheetDelegate, UIPickerViewDataSource>{
    SimulateActionSheet *sheet;
    NSArray* arrays;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    arrays = [NSArray arrayWithObjects:@"中国",@"美国",@"英国",@"日本",@"法国", nil];
}

- (IBAction)action:(id)sender {

    sheet = [SimulateActionSheet styleDefault];
    sheet.delegate = self;

    //必须在设置delegate之后调用,否则无法选中指定的行
    [sheet selectRow:arrays.count/2 inComponent:0 animated:YES];
    [sheet show:self];
}

-(void)actionCancle{
    [sheet dismiss:self];
}

-(void)actionDone{
    [sheet dismiss:self];

    NSUInteger index = [sheet selectedRowInComponent:0];

    NSLog(@"done with index of %lu",(unsigned long)index);
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return arrays.count;
}

-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [arrays objectAtIndex:row];
}

@end

2.SimulateActionSheet.h

//
//  SimulateActionSheet.h
//
#import <UIKit/UIKit.h>

@protocol SimulateActionSheetDelegate <UIPickerViewDelegate>
//点击取消的回调接口
-(void)actionCancle;
//点击确定的回调接口
-(void)actionDone;

@end

@interface SimulateActionSheet : UIView

@property(assign, nonatomic) id<SimulateActionSheetDelegate> delegate;
@property(strong, nonatomic)UIView* toolBar;
@property(strong, nonatomic)UIPickerView* pickerView;

+(instancetype)styleDefault;
-(void)show:(UIViewController *)controller;
-(void)dismiss:(UIViewController *)controller;
//选中指定的行列
-(void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)anime;
//获取被选中的行列
-(NSInteger)selectedRowInComponent:(NSInteger)component;

@end

3.SimulateActionSheet.m

//
//  SimulateActionSheet.m
//

#import "SimulateActionSheet.h"
#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]

@interface SimulateActionSheet(){
    UIColor *toolBarColor;
    UIColor *textColorNormal;
    UIColor *textColorPressed;
    UIColor *pickerBgColor;
}
@end

@implementation SimulateActionSheet
+(instancetype)styleDefault{
    SimulateActionSheet* sheet = [[SimulateActionSheet alloc]initWithFrame:CGRectMake(
                                                                                     0,
                                                                                     0,
                                                                                     UIScreen.mainScreen.bounds.size.width,
                                                                                     UIScreen.mainScreen.bounds.size.height)];

    [sheet setBackgroundColor:[UIColor clearColor]];
    sheet.toolBar = [sheet actionToolBar];
    sheet.pickerView = [sheet actionPicker];
    [sheet addSubview:sheet.toolBar];
    [sheet addSubview:sheet.pickerView];

    return sheet;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];

    if (self != nil) {
        toolBarColor = RGBACOLOR(240.0, 240.0, 240.0, 0.9);
        textColorNormal = RGBACOLOR(0, 146.0, 255.0, 1);
        textColorPressed = RGBACOLOR(209.0, 213.0, 219.0, 0.9);
        pickerBgColor = textColorPressed;
    }

    return self;
}
-(void)setupInitPostion:(UIViewController *)controller{
    [UIApplication.sharedApplication.keyWindow?UIApplication.sharedApplication.keyWindow:UIApplication.sharedApplication.windows[0]
                                                                              addSubview:self];
    [self.superview bringSubviewToFront:self];
    CGFloat pickerViewYpositionHidden = UIScreen.mainScreen.bounds.size.height;
    [self.pickerView setFrame:CGRectMake(self.pickerView.frame.origin.x,
                                         pickerViewYpositionHidden,
                                         self.pickerView.frame.size.width,
                                         self.pickerView.frame.size.height)];
    [self.toolBar setFrame:CGRectMake(self.toolBar.frame.origin.x,
                                      pickerViewYpositionHidden,
                                      self.toolBar.frame.size.width,
                                      self.toolBar.frame.size.height)];
}
-(void)show:(UIViewController *)controller{
    [self setupInitPostion:controller];

    CGFloat toolBarYposition = UIScreen.mainScreen.bounds.size.height -
    (self.pickerView.frame.size.height + self.toolBar.frame.size.height);

    [UIView animateWithDuration:0.25f
                     animations:^{
                         [self setBackgroundColor:[[UIColor darkGrayColor] colorWithAlphaComponent:0.5]];
                         [controller.view setTintAdjustmentMode:UIViewTintAdjustmentModeDimmed];
                         [controller.navigationController.navigationBar setTintAdjustmentMode:UIViewTintAdjustmentModeDimmed];

                         [self.toolBar setFrame:CGRectMake(self.toolBar.frame.origin.x,
                                                            toolBarYposition,
                                                            self.toolBar.frame.size.width,
                                                            self.toolBar.frame.size.height)];

                         [self.pickerView setFrame:CGRectMake(self.pickerView.frame.origin.x,
                                                     toolBarYposition+self.toolBar.frame.size.height,
                                                     self.pickerView.frame.size.width,
                                                     self.pickerView.frame.size.height)];
                     }
                     completion:nil];

}
-(void)dismiss:(UIViewController *)controller{
    [UIView animateWithDuration:0.25f
                     animations:^{
                         [self setBackgroundColor:[UIColor clearColor]];
                         [controller.view setTintAdjustmentMode:UIViewTintAdjustmentModeNormal];
                         [controller.navigationController.navigationBar setTintAdjustmentMode:UIViewTintAdjustmentModeNormal];
                         [self.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                             UIView* v = (UIView*)obj;
                             [v setFrame:CGRectMake(v.frame.origin.x,
                                                    UIScreen.mainScreen.bounds.size.height,
                                                    v.frame.size.width,
                                                    v.frame.size.height)];
                         }];
                     }
                     completion:^(BOOL finished) {
                         [self removeFromSuperview];
                     }];

}

-(UIView *)actionToolBar{
    UIView *tools = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, 44)];
    tools.backgroundColor = toolBarColor;
    UIButton *cancle = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    [cancle setTitle:@"取消" forState:UIControlStateNormal];
    [cancle setTitleColor:textColorPressed forState:UIControlStateHighlighted];
    [cancle setTitleColor:textColorNormal forState:UIControlStateNormal];
    [cancle addTarget:self action:@selector(actionCancle) forControlEvents:UIControlEventTouchUpInside];
    [cancle sizeToFit];
    [tools addSubview:cancle];

    cancle.translatesAutoresizingMaskIntoConstraints = NO;
    NSLayoutConstraint *cancleConstraintLeft = [NSLayoutConstraint constraintWithItem:cancle attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:tools attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f];
    NSLayoutConstraint *cancleConstrainY = [NSLayoutConstraint constraintWithItem:cancle attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:tools attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0];
    [tools addConstraint:cancleConstraintLeft];
    [tools addConstraint:cancleConstrainY];

    UIButton *ok = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    [ok setTitle:@"确定" forState:UIControlStateNormal];
    [ok setTitleColor:textColorNormal forState:UIControlStateNormal];
    [ok setTitleColor:textColorPressed forState:UIControlStateHighlighted];
    [ok addTarget:self action:@selector(actionDone) forControlEvents:UIControlEventTouchUpInside];
    [ok sizeToFit];
    [tools addSubview:ok];

    ok.translatesAutoresizingMaskIntoConstraints = NO;
    NSLayoutConstraint *okConstraintRight = [NSLayoutConstraint constraintWithItem:ok attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:tools attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-10.0f];
    NSLayoutConstraint *okConstraintY = [NSLayoutConstraint constraintWithItem:ok attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:tools attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0];
    [tools addConstraint:okConstraintRight];
    [tools addConstraint:okConstraintY];

    return tools;
}

-(UIPickerView *)actionPicker;{
    UIPickerView *picker = [[UIPickerView alloc]initWithFrame:CGRectMake(self.pickerView.frame.origin.x,
                                                                         self.bounds.size.height,
                                                                         self.bounds.size.width,
                                                                         216)];
    picker.showsSelectionIndicator=YES;
    [picker setBackgroundColor:pickerBgColor];

    return picker;
}

-(void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)anime{
    [_pickerView selectRow:row inComponent:component animated:anime];
}

-(NSInteger)selectedRowInComponent:(NSInteger)component{
    return [_pickerView selectedRowInComponent:component];
}

-(void)actionDone{
    if([_delegate respondsToSelector:@selector(actionDone)]){
        [_delegate actionDone];
    }
}

-(void)actionCancle{
    if ([_delegate respondsToSelector:@selector(actionCancle)]) {
        [_delegate actionCancle];
    }
}
-(void)setDelegate:(id<SimulateActionSheetDelegate>)delegate{
    _delegate = delegate;
    _pickerView.delegate = delegate;
}
@end

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

[小白学iOS编程04]UIDatePicker控件学习(键盘处理)

[小白学iOS编程04]UIDatePicker控件学习(键盘处理) 本博文通过一个简单的例子来学习UIDatePicker中常见的方法。...

点击textField弹出自定义pickerView的完整解决方案

这几天做一个应用有这样一个需求,有一个'新建收货地址'的页面(tableView),其中有一个textField是输入城市的,要求是点击textField(输入框)后,下面弹出来一个自定义的带有too...
  • qfeung
  • qfeung
  • 2016年09月12日 17:15
  • 2676

【iOS】点击UIButton,弹出UIPickerView

不需要讲UIPickerView 作为 subview : 做一个隐藏的 UITextField ViewController.m #import "View...
  • scuhmz
  • scuhmz
  • 2014年12月18日 16:38
  • 1803

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

android DatePicker 点击文本框,禁止弹出键盘

相信很多同行,都使用过android 自带的日期或者时间控件,在api 14(记不清是哪个版本)以上的不会有这个错误,ui是直接拖动的,但14以下的版本是类似于这样的:       ...

写一个可以快速弹出多种滚轮选择器(PickerView)的小工具

使用示例效果TextField使用示例按钮使用示例(gif效果不满意)可以简单快速的实现上图中的效果使用方法1. TextField支持xib和代码生成使用, 只需调用一个方法, 设置选择的数据, 和...

iOS学习笔记01-自定义简单弹出pickerView

UIPickerView平常用的地方好像也不是很多,顶多就是一些需要选择的地方,这次项目需要这一个功能,我就单独写了一个简单的demo,效果图如下:新增主页面弹出view,在主页面添加的代码有个小问题...

pickerview实现底部弹出选择时间

首先看一下效果是否是您需要的呢 首先导入依赖 compile 'com.bigkoo:pickerview:2.0.7' 上面放选择的数据时,这里使用的是Button...

IOS_DatePicker_PickerView_SegmentControl_键盘处理

H:/0712/01_UIController_MJViewController.m// MJViewController.m // 01-总结复习 // Created by apple on...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:键盘弹出-PickerView
举报原因:
原因补充:

(最多只允许输入30个字)