2011-08-21 89 views
0

iOS的衰落動畫我已經在時間,這每一個視圖被裝載iOS應用程序,都在視圖中的對象很快消失英寸多個對象

在我的代碼,我有一個

- (void)fadeInEverything 

功能,而這個功能中,我把五個不同的功能,對他們每個人一個NSTimer,使對象在連續褪色,就像這樣:

[self fadeInLabel]; 


[NSTimer scheduledTimerWithTimeInterval:0.2 
           target:self 
           selector:@selector(fadeInTextField) 
           userInfo:nil 
           repeats:NO]; 
[NSTimer scheduledTimerWithTimeInterval:0.4 
           target:self 
           selector:@selector(fadeInButton) 
           userInfo:nil 
           repeats:NO]; 
[NSTimer scheduledTimerWithTimeInterval:0.6 
           target:self 
           selector:@selector(fadeInTextView) 
           userInfo:nil 
           repeats:NO]; 
[NSTimer scheduledTimerWithTimeInterval:0.8 
           target:self 
           selector:@selector(fadeInFlipsideViewButton) 
           userInfo:nil 
           repeats:NO]; 

我有一個看起來LIK功能E本爲每個對象:

- (void)fadeInTextView 
{ 
    [resultView setAlpha:0]; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:1]; 

    [resultView setAlpha:1]; 

    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 
    [UIView commitAnimations]; 
} 

我的問題是,會不會有我們的方式進行,將採取一個參數(對象),並消除它一個功能,所以我不必有五個幾乎相同的功能?這會爲我節省很多空間和雜亂。

回答

2

您可以通過引用傳遞的對象來實現這一目標:

-(void)fadeInView:(UIView **)aView { // the ** indicates that this method is expecting a reference 
    // your fading code 
    // this works, because all view's (UILabels, UITextFields etc. inherit UIView. 
} 

,並且調用方法看起來是這樣的:

-(void)callFader { 
    [self fadeInView:&mytextView]; // make sure to put the & before the variable so that it's sent as a reference 
    // and more fadeInView calls...  
} 
+0

這並沒有爲我工作...我得到一個分段故障 –