3

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

谢谢你。

4

3 回答 3

17

所有UIResponder对象都有一个inputView属性。inputViewa是当响应者成为第一响应者时将代替键盘显示的UIResponder视图。

因此,如果您希望 aUIPickerView出现而不是键盘,您可以简单地通过使您的UIResponder(如 a UITextField)具有 aUIPickerView作为它的inputView.

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

于 2013-09-17T04:31:31.997 回答
0

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

于 2013-09-17T04:33:46.060 回答
0

hidepicker 和 showpicker 方法的想法很好,“UIPicker 在应用程序加载时可见”的问题可以通过设置 UIPickerView 的框架同时将其初始化到不可见的位置来解决......之后您可以调用 showpicker 方法来显示选择器视图。

于 2013-09-17T04:47:07.583 回答