2013-01-14 89 views
1

我只是一個新手,所以不知道這是否是正確的地方問。顏色循環Xcode UIView背景

我發現這個代碼在您的網站做一個Xcode iPhone應用程序在背景色週期 - UIView backgroundColor color cycle

和它的偉大工程!我怎麼調整它,所以它只是循環一次,並結束在數組中的最後一個顏色?

謝謝你的幫助。

// ViewController.m 
// colorCycle 

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self doBackgroundColorAnimation]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (void) doBackgroundColorAnimation { 
    static NSInteger i = 0; 
    NSArray *colors = [NSArray arrayWithObjects:[UIColor greenColor], [UIColor yellowColor], [UIColor orangeColor], [UIColor redColor], nil]; 

    if(i >= [colors count]) { 
     i = 0; 
    } 

    [UIView animateWithDuration:2.0f animations:^{ 
     self.view.backgroundColor = [colors objectAtIndex:i]; 
    } completion:^(BOOL finished) { 
     ++i; 
     [self doBackgroundColorAnimation]; 
    }]; 

} 

回答

2

您可以在if塊中添加return語句。

- (void) doBackgroundColorAnimation { 
static NSInteger i = 0; 
NSArray *colors = [NSArray arrayWithObjects:[UIColor greenColor], [UIColor yellowColor], [UIColor orangeColor], [UIColor redColor], nil]; 

if(i >= [colors count]) { 
    return; 
} 

[UIView animateWithDuration:2.0f animations:^{ 
    self.view.backgroundColor = [colors objectAtIndex:i]; 
} completion:^(BOOL finished) { 
    ++i; 
    [self doBackgroundColorAnimation]; 
}]; 

}