颜色循环Xcode UIView背景

时间:2013-01-14 06:14:48

标签: xcode uiview colors background-color cycle

我只是一个新手,所以不确定这是否是正确的问题。

我在您的网站上发现了这个代码来创建一个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];
    }];

}

1 个答案:

答案 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];
}];

}