iOS - 使用UIToolbar进行动画故障

时间:2012-07-28 19:34:52

标签: objective-c animation uitoolbar

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

我注意到如果我在上滑调用中设置延迟(- performSelector...)0.3秒,它将正确显示UIToolbar(任何小于0.3秒的内容仍然会有相同的故障)。可能是什么导致了这个?

编辑:也许我应该将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;

    }];
}

1 个答案:

答案 0 :(得分:0)

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