2010-02-22 66 views
1

我試圖從屏幕上以動畫模式移除超級視圖中的視圖。但是,在動畫應該結束之後,當我添加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]; 
} 
+0

你是說,如果你註釋掉與「removeFromSuperview」行,那麼動畫的作品?那會很奇怪。你有沒有嘗試過:設置一個更長的animationDuration,或者檢查委託方法中的「完成」值?不是說這些中的任何一個都會導致你描述的問題,但可能會產生更多的信息。 – Felixyz 2010-02-22 23:13:58

+0

事實上,當我註釋掉'removeFromSuperview'這一行時,動畫就起作用了。在動畫開始之前,視圖立即從視圖中移除。通過KVO觀察動畫視圖,因此可以看到removeObserver行,但我不認爲這些行對這個問題很重要。 – Oysio 2010-02-23 08:15:11

回答

0

好吧,我最終發現是什麼原因導致動畫無法啓動。 pickerView被觀察到,每當有一個,如果它的實例變量會改變值,父視圖會得到通知,並開始動畫滑動pickerview離屏。 但是,由於父視圖同時觀察舊值和新值,因此它會得到兩次通知,首先是舊的,然後是新的更改的值。因此,父視圖也一次又一次地開始動畫,顯然這導致動畫根本無法啓動。

固定的這個樣子,只是註釋掉這是送舊值每次ValueSelectDone其觀察員FIRSTLINE正在發生變化值:

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

valueSelectDone = flag; 

[self didChangeValueForKey:@"valueSelectDone"]; 
相關問題