2012-06-08 37 views
0

我有一個小問題,我試圖顯示與這樣的動畫視圖:beginAnimations:子視圖的動畫慢然後視圖的動畫

self.vistaAiuti = [[UIView alloc] initWithFrame:CGRectMake(10, -200, 300, 200)]; 
self.vistaAiuti.backgroundColor = [UIColor blueColor]; 


UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
closeButton.frame = CGRectMake(50, 140, 220, 40); 
[closeButton setTitle:@"Go back" forState:UIControlStateNormal]; 
[closeButton addTarget:self action:@selector(closeView:) forControlEvents:UIControlEventTouchUpInside]; 

[self.vistaAiuti addSubview:closeButton];  
[self.view addSubview:self.vistaAiuti]; 

[UIView beginAnimations:@"MoveView" context:nil]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
[UIView setAnimationDuration:0.5f]; 
self.vistaAiuti.frame = CGRectMake(10, 0, 300, 200); 
[UIView commitAnimations]; 

爲接近它,這樣的:

[UIView beginAnimations:@"MoveView" context:nil]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
[UIView setAnimationDuration:0.5f]; 
self.vistaAiuti.frame = CGRectMake(10, -200, 300, 0); 
[UIView commitAnimations]; 

問題是vista aiuto上的按鈕比vistaAiuti更慢,所以當我關閉視圖時,按鈕會保留一段時間......我需要做什麼以獲得相同的速度?

+2

請填寫的問題。 – danh

+0

對不起,我失去了一些單詞:(現在已完成! – kikko088

回答

3

問題是vistaAiuti框架在關閉動畫中被設置爲零高度。該按鈕似乎滯後,但實際發生的情況是,下方的父視圖縮小到零高度-200原點。

更改關閉動畫對象幀到:

self.vistaAiuti.frame = CGRectMake(10, -200, 300, 200); 

此外,一些其他的建議:

獨立創建從顯示它的視圖。這樣,每次你想顯示它時你都不會再添加子視圖。

- (void)addVistaAiutiView { 

    // your creation code 
    self.vistaAiuti = [[UIView alloc] initWithFrame:CGRectMake(10, -200, 300, 200)]; 
    self.vistaAiuti.backgroundColor = [UIColor blueColor]; 

    UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    closeButton.frame = CGRectMake(50, 140, 220, 40); 
    [closeButton setTitle:@"Go back" forState:UIControlStateNormal]; 
    [closeButton addTarget:self action:@selector(closeView:) forControlEvents:UIControlEventTouchUpInside]; 

    [self.vistaAiuti addSubview:closeButton];  
    [self.view addSubview:self.vistaAiuti]; 
} 

使用塊動畫,它的更緊湊的編寫和易於閱讀

- (BOOL)vistaAiutiIsHidden { 

    return self.vistaAiuti.frame.origin.y < 0.0; 
} 

- (void)setVistaAiutiHidden:(BOOL)hidden animated:(BOOL)animated { 

    if (hidden == [self vistaAiutiIsHidden]) return; // do nothing if it's already in the state we want it 

    CGFloat yOffset = (hidden)? -200 : 200;   // move down to show, up to hide 
    NSTimeInterval duration = (animated)? 0.5 : 0.0; // quick duration or instantaneous 

    [UIView animateWithDuration:duration animations: ^{ 
     self.vistaAiuti.frame = CGRectOffset(0.0, yOffset); 
    }]; 
} 
+0

非常感謝你!解決了我的問題,並學習了一個新的瘦身! – kikko088