从superView中删除视图时不会显示动画

时间:2010-02-22 22:50:17

标签: iphone objective-c cocoa-touch

我尝试在屏幕外动画后从其超级视图中删除视图。但似乎当我在动画结束后添加removeFromSuperview调用时,视图根本没有动画,而是立即从屏幕上消失。
那么为什么在我将[[self pickerView] removeFromSuperview];添加到下面的方法时没有动画?

- (void) slidePickerOutView
{

    [UIView beginAnimations:@"slidePickerOutView" context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.2];
    [UIView setAnimationDidStopSelector:@selector(slidePickerOutViewEnded:finished:context:)];

    CGRect r = CGRectMake(0, 480, 320, 244);

    [[self pickerView] setFrame:r];

    [UIView commitAnimations];
}

- (void) slidePickerOutViewEnded:(NSString *)id finished:(BOOL) finished context:(void *) context 
{
    //Stop observing the 'Done' button
    [[self pickerView] removeObserver:self forKeyPath:@"valueSelectDone"];
    [[self pickerView] removeObserver:self forKeyPath:@"selectedValue"];

    [[self pickerView] removeFromSuperview];

    [self setPickerView:nil];
}

1 个答案:

答案 0 :(得分:0)

好的,我最终找到了导致动画无法启动的原因。正在观察pickerView,只要它的实例变量值发生变化,父视图就会得到通知并启动动画以将pickerview滑动到屏幕外。 但是由于parentView正在观察旧值和新值,因此它会被通知两次,首先是旧的,然后是新的更改值。因此,parentView也会相继启动动画两次,显然这导致动画根本没有启动。

这样修复,只需通过注释掉每次ValueSelectDone更改值时将旧值发送给其观察者的第一行:

//[self willChangeValueForKey:@"valueSelectDone"];

valueSelectDone = flag;

[self didChangeValueForKey:@"valueSelectDone"];