gpt4 book ai didi

ios - 如何像 iOS 键盘一样显示和隐藏 UIPickerView?

转载 作者:可可西里 更新时间:2023-11-01 06:18:16 26 4
gpt4 key购买 nike

我有这段代码默认隐藏 UIPickerView:

- (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 对象都有一个 inputView属性(property)。 UIResponderinputView 是将在响应器 becomes the first responder代替键盘显示的 View 。 .

因此,如果您希望显示 UIPickerView 而不是键盘,您可以简单地通过制作 UIResponder(如 UITextField) 有一个 UIPickerView 作为它的 inputView

(作为一个警告:您可能不希望将裸UIPickerView 作为inputView,因为您还需要考虑键盘何时会改变大小,例如当你旋转时。但这是一般的想法。)

关于ios - 如何像 iOS 键盘一样显示和隐藏 UIPickerView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18841239/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com