iOS – UIToolbar 的动画故障
Posted
技术标签:
【中文标题】iOS – UIToolbar 的动画故障【英文标题】:iOS – Animation glitch with UIToolbar 【发布时间】:2012-07-28 19:34:52 【问题描述】:当我将UIToolbar
添加到视图时,我遇到了一个“奇怪”的动画故障。我正在为 UIToolbar 设置动画,使其与UIPickerView
或UIDatePicker
一起从底部向上滑动。起初我看到它在向上滑动,但动画完成后它很快就消失了。当我向下滑动(动画 UIPickerView 以从屏幕上向下滑动)时会发生这种情况,然后我又有另一个 UIPickerView 向上滑动。
我注意到,如果我在上滑调用上设置延迟 (- performSelector...
) 0.3 秒,它将正确显示 UIToolbar(小于 0.3 秒的任何内容仍会出现相同的故障)。这可能是什么原因造成的?
编辑:也许我应该将UIToolbar
和UIDatePicker
都放在一个新的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】:通过将UIPickerView
和UIToolbar
添加到包含UIView
然后为包含的UIView 设置动画来解决。
【讨论】:
以上是关于iOS – UIToolbar 的动画故障的主要内容,如果未能解决你的问题,请参考以下文章
UIToolbar 的动画调整大小导致在 iOS <5.1 上剪切背景
在 UIToolbar 中的 UIBarButtonItem 上更改 tintColor 会导致按钮消失并从左侧动画化