【问题标题】:How to Show and Hide UIPickerView like iOS Keyboard?如何像 iOS 键盘一样显示和隐藏 UIPickerView?
【发布时间】:2013-09-17 12:54:46
【问题描述】:

我有这段代码默认隐藏 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 已经显示出来了。第一次加载时如何隐藏?

谢谢。

【问题讨论】:

  • 使用操作表显示选择器视图

标签: ios objective-c


【解决方案1】:

所有UIResponder 对象都有一个inputView 属性。 UIResponderinputView 是当响应者 becomes the first responder 时将显示的视图代替键盘

因此,如果您希望显示UIPickerView 而不是键盘,您可以简单地通过使您的UIResponder(如UITextField)具有UIPickerView 作为其inputView 来实现。

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

【讨论】:

    【解决方案2】:

    viewDidLoad 中获取一个布尔变量并将其值设置为TRUE 并设置UIPickerView 的帧,以便UIPickerView 第一次不可见。基于布尔值处理帧动画显示或隐藏选择器视图。

    【讨论】:

      【解决方案3】:

      hidepicker 和 showpicker 方法思路不错,只要设置 UIPickerView 的框架,同时将其初始化到不可见的位置,就可以解决“加载应用时 UIPicker 可见”的问题。 . 之后你可以调用 showpicker 方法来显示选择器视图。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-21
        • 2016-04-10
        • 1970-01-01
        相关资源
        最近更新 更多