2011-11-08 33 views
0

我想用下面的動畫去除子視圖。但是,當我點擊一個按鈕來運行代碼時,視圖立即被刪除。有誰知道這裏發生了什麼。用ainimation去除子視圖

感謝,

CGRect rect=[self.view viewWithTag:10].frame; 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:1]; 
     [UIView setAnimationDidStopSelector:@selector(removeLayer)]; 
     [[self.view viewWithTag:10] setFrame:CGRectMake(rect.origin.x, btn.frame.origin.y, rect.size.width, 0)]; 
     [UIView animateWithDuration:1.0 animations:^{[[self.view viewWithTag:10] setFrame:CGRectMake(rect.origin.x, btn.frame.origin.y, rect.size.width, 0)];} completion:^(BOOL finished){[[self.view viewWithTag:10] removeFromSuperview];}]; 
     [UIView commitAnimations]; 


-(void)removeLayer{ 
    [[self.view viewWithTag:10] removeFromSuperview]; 
} 
+0

不要忘了委託;)'[UIView的setAnimationDelegate:自我]' – beryllium

回答

1

它看起來像你試圖同時使用兩種不同的動畫方法。所有你需要的是:

CGRect rect=[self.view viewWithTag:10].frame; 

[UIView animateWithDuration:1.0 animations:^{[[self.view viewWithTag:10] setFrame:CGRectMake(rect.origin.x, btn.frame.origin.y, rect.size.width, 0)];} completion:^(BOOL finished){[[self.view viewWithTag:10] removeFromSuperview];}]; 
+0

哦,是的,是我的錯。我忘記刪除其中的一種方法。 –

1

要結合這兩種類型的動畫API的 - 我認爲,基於塊的一個被立即返回所以你的didStopSelector被稱爲立竿見影。

儘量只使用這部分內容:

[UIView animateWithDuration:1.0 animations:^{[[self.view viewWithTag:10] setFrame:CGRectMake(rect.origin.x, btn.frame.origin.y, rect.size.width, 0)];} completion:^(BOOL finished){[[self.view viewWithTag:10] removeFromSuperview];}]; 
0

我認爲你忘了設置動畫的委託:

[UIView setAnimationDelegate:self]; 

在代碼中的UIView不知道哪個對象應該發「removeLayer」選擇器