颜色循环Xcode UIView背景

问题描述:

我只是一个新手,所以不知道这是否是正确的地方问。颜色循环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]; 
    }]; 

} 

您可以在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]; 
}]; 

}