2011-07-21 63 views
0

我是iphone開發新手。我正在製作的應用使用選取器從用戶輸入值。我設法隱藏了選取器,直到用戶點擊按鈕。我在viewdidload中使用了mypicker.alpha = 0;,所以程序啓動時選擇器不可見。當用戶點擊開始按鈕時,它執行代碼mypicker.alpha=1;。我想在用戶選擇一個值後關閉選取器。我怎麼做?任何人有任何提示或教程?我看了幾個,但他們混亂!另外我如何讓自動選取器從底部出現? (如鍵盤!)在iphone應用程序中關閉UIpicker

回答

1

我最近開始使用的一種方法是在選取器後面放置一個陰影按鈕,一個大的透明黑色按鈕,屏幕大小爲黑色,alpha = 0.3([UIColor colorWithWhite:0 alpha :0.3f]我認爲是)。這只是在屏幕的其餘部分上放置一個透明的「陰影」,除了選取器之外,類似於使用UIAlertView時的外觀。然後鉤上按鈕,以便它將resignFirstResponder發送到選取器。現在,當用戶完成拾取時,他們只需點擊陰影區域內選擇器外的任何位置,然後該按鈕就會使拾取器退出,並且拾取器可以向下滑動,並且按鈕會通過動畫淡出。

選擇器滑動上/下動畫可以完成,我在家裏有它的代碼,但現在無法訪問它。您可以使其看起來像鍵盤一樣發送與鍵盤發送相同的通知。

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; { 

} 

很明顯,你可以設置選擇器的任何您想要的方式。您也可以改變發生這種情況的速度!希望這可以幫助!