在iPhone应用程序中关闭UIpicker

时间:2011-07-21 20:52:52

标签: objective-c ios4

我是iphone dev的新手。我正在制作的应用程序使用选择器从用户输入值。我设法隐藏了选择器,直到用户点击按钮。我在viewdidload中使用了mypicker.alpha = 0;,因此当程序启动时,选择器是不可见的。当用户点击开始按钮时,它执行代码mypicker.alpha=1;。我希望在用户选择值后关闭选择器。我怎么做?有没有任何提示或教程?我看了几眼,但他们很困惑!另外如何使拾取器从下往上显示? (就像键盘一样!)

2 个答案:

答案 0 :(得分:1)

我最近开始使用的一种方法是在拾取器后面放一个阴影按钮,一个大屏幕的透明黑色按钮,颜色为黑色,alpha = 0.3([UIColor colorWithWhite:0 alpha:0.3f]我认为它是)。除了拾取器之外,这只是在屏幕的其余部分放置一个透明的“阴影”,类似于使用UIAlertView时的外观。然后挂钩按钮,以便它将resignFirstResponder发送给选择器。现在当用户完成拾取时,他们只需点击阴影区域中拾取器外的任何位置,按钮就会重新选择拾取器,拾取器可以向下滑动,按钮会以动画淡出。

拾取器上下滑动动画可以完成,我在家里有代码,但现在无法访问它。您可以使它看起来像键盘一样,并发送键盘发送的相同通知。

答案 1 :(得分:0)

不要使用:

mypicker.alpha = 1;
mypicker.alpha = 0;

您应该使用:

mypicker.hidden = YES; 
mypicker.hidden = NO;

以显示或隐藏选择器。

为了使其从底部显示,您可以使用块动画。我会用:

.h文件:

@interface viewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
  BOOL shouldMoveDown;
  IBOutlet UIPickerView * picker;
}

- (IBAction)movePicker;

.m文件:

#pragma mark - View lifecycle

- (void)viewDidLoad; {
  [super viewDidLoad];
  picker.hidden = YES;
  shouldMoveDown = NO;
  picker.userInteractionEnabled = NO;
}

- (IBAction)movePicker; {
  if(shouldMoveDown){
    [UIView animateWithDuration:1.0
                     animations:^{ 
                       CGRect newRect = picker.frame;
                       newRect.origin.y += 236; // 480 - 244 (Height of Picker) = 236
                       picker.frame = newRect;
                     } 
                     completion:^(BOOL finished){

                       [UIView animateWithDuration:1.0
                                        animations:^{ 
                                          picker.hidden = YES;
                                          shouldMoveDown = NO;
                                          picker.userInteractionEnabled = NO;
                                        } 
                                        completion:^(BOOL finished){
                                          ;
                                        }];
                     }];
  }
  else{
    picker.hidden = NO;
    //picker.frame = CGRectMake(picker.frame.origin.x, 480, picker.frame.size.width, picker.frame.size.height);
    [UIView animateWithDuration:1.0
                     animations:^{ 
                       CGRect newRect = picker.frame;
                       newRect.origin.y -= 236; // 480 - 244 (Height of Picker) = 236
                       picker.frame = newRect;
                     } 
                     completion:^(BOOL finished){

                       [UIView animateWithDuration:1.0
                                        animations:^{ 
                                          shouldMoveDown = YES;
                                          picker.userInteractionEnabled = YES;
                                        } 
                                        completion:^(BOOL finished){
                                          ;
                                        }];
                     }];
  }
}

#pragma mark -
#pragma mark Picker Delegate Methods

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

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component; {

    return 1;
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; {
  return @"1";
}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component; {

}

显然你可以按照你想要的方式设置选择器。您也可以改变发生这种情况的速度!希望这有帮助!