我只是一个新手,所以不确定这是否是正确的问题。
我在您的网站上发现了这个代码来创建一个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];
}];
}
答案 0 :(得分: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];
}];
}