如何显示和隐藏像iOS键盘一样的UIPickerView?
默认情况下,我有以下代码隐藏UIPickerView:如何显示和隐藏像iOS键盘一样的UIPickerView?,ios,objective-c,Ios,Objective C,默认情况下,我有以下代码隐藏UIPickerView: - (void)viewDidLoad { [super viewDidLoad]; [_memberList setAlpha:0]; } 点击按钮时显示UIPickerView的代码: - (IBAction)buttonChooseMember { [UIView animateWithDuration:0.6 delay:0. options:UIViewAnimationOptionCurveEa
- (void)viewDidLoad
{
[super viewDidLoad];
[_memberList setAlpha:0];
}
点击按钮时显示UIPickerView的代码:
- (IBAction)buttonChooseMember {
[UIView animateWithDuration:0.6 delay:0. options:UIViewAnimationOptionCurveEaseInOut animations:^{
[_memberList setAlpha:1];
} completion:nil];
}
最后一件事是,当用户点击任意位置时隐藏键盘:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UIView * txt in self.view.subviews){
if ([txt isKindOfClass:[UITextField class]]) {
[txt resignFirstResponder];
}else if ([txt isKindOfClass:[UIPickerView class]]) {
[UIView animateWithDuration:0.6 delay:0. options:UIViewAnimationOptionCurveEaseInOut animations:^{
[_registerMLMList setAlpha:0];
} completion:nil];
}
}
}
但所有这些只是给我“出现”动画,因为它只是将Alpha值从0更改为1(反之亦然)。不像iOS键盘那样向上或向下滑动
我尝试使用下面的动画在UIPickerView上显示iOS键盘外观:
- (IBAction)hidePicker {
UIPickerView *pickerView = [[UIPickerView alloc] init]; // default frame is set
float pvHeight = pickerView.frame.size.height;
float y = _screen.bounds.size.height - (pvHeight * -2); // the root view of view controller
[UIView animateWithDuration:0.5f delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.picker.frame = CGRectMake(0 , y, pickerView.frame.size.width, pvHeight);
} completion:nil];
}
- (IBAction)showPicker {
UIPickerView *pickerView = [[UIPickerView alloc] init]; // default frame is set
float pvHeight = pickerView.frame.size.height;
float y = _screen.bounds.size.height - (pvHeight); // the root view of view controller
[UIView animateWithDuration:0.5f delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.picker.frame = CGRectMake(0 , y, pickerView.frame.size.width, pvHeight);
} completion:nil];
}
我喜欢这个动画,它看起来像iOS键盘动画,但这个动画的问题是。。。加载我的应用程序时,UIPickerView已显示。第一次加载时如何隐藏它
谢谢。所有
UIResponder
对象都有一个属性。ui响应程序的inputView
是响应程序启动时显示在键盘位置的视图
因此,如果希望显示UIPickerView
而不是键盘,只需将UIResponder
(如UITextField
)的UIPickerView
作为其输入视图即可
(作为警告:您可能不希望将裸UIPickerView
作为inputView
,因为您还需要考虑键盘何时会改变大小,比如旋转。但这是一般的想法。)在viewDidLoad
中,取一个布尔变量,将其值设置为TRUE
,并设置UIPickerView
的帧,使UIPickerView
第一次不可见。基于布尔值,处理帧动画以显示或隐藏选择器视图。hidepicker和showpicker方法的思想是很好,而且“加载应用程序时UIPicker可见”的问题可以通过设置UIPickerView的框架,同时将其启动到不可见的位置来解决……之后,您可以调用showpicker方法来显示picker视图。使用操作表来显示picker视图