1

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

我在你的网站上找到了这段代码来制作一个 xcode iphone 应用程序,其中背景颜色循环 - UIView backgroundColor 颜色循环

效果很好!我怎样才能调整它,使它只循环一次并以数组中的最后一种颜色结束?

谢谢你的帮助。

//  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];
    }];

}
4

1 回答 1

2

您可以return在 if 块中添加一条语句。

- (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];
}];

}

于 2013-01-14T06:46:19.427 回答