1

当我将 a 添加UIToolbar到视图时,我遇到了一个“奇怪的”动画故障。我正在为 UIToolbar 设置动画,因此它与 aUIPickerView或 a一起从底部向上滑动UIDatePicker。起初我看到它在向上滑动,但动画完成后它很快就消失了。当我向下滑动(动画 UIPickerView 以从屏幕上向下滑动)时会发生这种情况,然后我又有另一个 UIPickerView 向上滑动。

我注意到,如果我- performSelector...在上滑调用中设置延迟(这可能是什么原因造成的?

编辑:也许我应该将UIToolbarUIDatePicker放在一个新UIView容器中?

这是我正在使用的代码:

if ([self.view.subviews containsObject:self.dateRepeatPicker]) {

        [self dismissDateRepeatPickerSegmentChanged:NO];

         // "Hack", if I don't delay this call the UIToolbar will not display
         [self performSelector:@selector(showDatePicker) withObject:nil afterDelay:0.3];

} else if (![self.view.subviews containsObject:self.datePicker]) {

         [self showDatePicker];
}


- (void)dismissDateRepeatPickerSegmentChanged:(BOOL)segmentChanged {

    CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height, 320, 44);
    CGRect dateRepeatPickerTargetFrame = CGRectMake(0, self.view.bounds.size.height+44, 320, 216);

    [UIView animateWithDuration:0.2 

                     animations:^{
                         self.pickerToolbar.frame = toolbarTargetFrame;
                         self.dateRepeatPicker.frame = dateRepeatPickerTargetFrame;
                     }

                     completion:^(BOOL finished) {

                         if (segmentChanged) {

                             [self.pickerToolbar removeFromSuperview];
                             [self.dateRepeatPicker removeFromSuperview];
                             [self.remindMeTableView reloadData];

                         } else {

                             [self.pickerToolbar removeFromSuperview];
                             [self.dateRepeatPicker removeFromSuperview];
                         }

                     }];
}

- (void)showDatePicker {


    // Create the Toolbar over the Picker
    CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height-216-44, 320, 44);

    if (self.pickerToolbar == nil) {

        self.pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
        self.pickerToolbar.barStyle = UIBarStyleBlack;
        self.pickerToolbar.translucent = YES;
    }

    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"picker-dismiss.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(dismissDatePickerToolbar)];
    [self.pickerToolbar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
    [self.view addSubview:self.pickerToolbar];


    // Create the Picker under the Toolbar
    CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height-216, 320, 216);

    if (self.datePicker == nil) {

        self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 216)];
        [self.datePicker setMinuteInterval:5];

        [self.datePicker addTarget:self action:@selector(changeDate:) forControlEvents:UIControlEventValueChanged];

    }

    [self.view addSubview:self.datePicker];

    [UIView animateWithDuration:0.2 animations:^{

        self.pickerToolbar.frame = toolbarTargetFrame;
        self.datePicker.frame = datePickerTargetFrame;

    }];
}
4

1 回答 1

0

通过添加UIPickerViewUIToolbar到包含UIView然后为包含的 UIView 设置动画来解决。

于 2012-07-28T23:45:14.537 回答