Bottom pop-up UIPicker?

后端 未结 4 1371
挽巷
挽巷 2020-12-29 00:36

Is the UIPicker that pops up from the bottom of the screen when an action is called just a basic UIPickerView that is coordinated in a certain way? (Like a UIActionSheet?)

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-29 01:19

    Here's animation code, that I use:

    - (void)animateDatePicker:(BOOL)show {
        CGRect screenRect = self.frame;
        CGSize pickerSize = [self.datePickerView sizeThatFits:CGSizeZero];
        CGRect startRect = CGRectMake(0.0,
                                      screenRect.origin.y + screenRect.size.height,
                                      pickerSize.width, pickerSize.height);
    
        CGRect pickerRect = CGRectMake(0.0,
                                       screenRect.origin.y + screenRect.size.height - pickerSize.height,
                                       pickerSize.width,
                                       pickerSize.height);
    
        self.datePickerView.frame = pickerRect;
        self.backgroundColor = UIColorMakeRGBA( 64, 64, 64, 0.7f - (int)show * 0.7f );
    
        if ( show ) {
            self.datePickerView.frame = startRect;
            [self.parentViewController addSubviewToWindow:self];
        }
    
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3f];
        [UIView setAnimationDelegate:self];
    
        self.backgroundColor = UIColorMakeRGBA( 64, 64, 64, 0.0f + (int)show * 0.7f );
    
        if ( show ) {
            self.datePickerView.frame = pickerRect;
        } else {
            [UIView setAnimationDidStopSelector:@selector(slideDownDidStop)];
            self.datePickerView.frame = startRect;
        }
    
        [UIView commitAnimations];  
    }
    

    datePickerView is a view, that contains DatePicker:

    self.datePickerView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 174.0, 320.0, 286.0)];
    self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 70.0, 320.0, 216.0)];
    [self.datePicker setDatePickerMode:UIDatePickerModeDate];
    [self.datePicker addTarget:self action:@selector(pickerValueChanged:) forControlEvents:UIControlEventValueChanged];
    [self.datePickerView addSubview:self.datePicker];
    

    parentViewController should implement addSubviewToWindow method:

    - (void)addSubviewToWindow:(UIView *)addView {
        [self.view addSubview:addView];
    }
    

    UIColorMakeRGBA:

    #define UIColorMakeRGBA(nRed, nGreen, nBlue, nAlpha) [UIColor colorWithRed:(nRed)/255.0f green:(nGreen)/255.0f blue:(nBlue)/255.0f alpha:nAlpha]
    

    slideDownDidStop is a method, that will be called after datePicker slide down successfully.

    So, just to summarize - you have MyViewController, that have

    MyDatePickerView *myDatePicker;
    

    field.

    MyDatePickerView is a custom class, that have UIDatePicker *datePicker field, MyViewController *parentViewController and animateDatePicker method.

    When you perform some action on MyViewController (for example, UIControlEventTouchUpInside for some button), you should invoke

    [myDatePicker animateDatePicker:YES];
    

    Please let me know if you have questions.

    UPDATE: Here's a small example.

提交回复
热议问题