点对点动画

Point to Point Animation

http://postimg.org/image/8bwxdp4bj/7124c28f/ 我想制作上图所示的动画。我正在使用这段代码。它们移动到无穷大,我的视图不会出现,因为它们涉及 for 循环。我是 iOS 的新人。请 help.what 我是 stuck.Any 帮助将不胜感激。

for (i=0; i<3; i++) {
        if (i==0) {
           first.backgroundColor=[UIColor blackColor];
           second.backgroundColor=[UIColor yellowColor];
            third.backgroundColor=[UIColor yellowColor];
            button.backgroundColor=[UIColor yellowColor];
            i=1;
        }
        if (i==1) {
            first.backgroundColor=[UIColor yellowColor];
            second.backgroundColor=[UIColor blackColor];
            third.backgroundColor=[UIColor yellowColor];
            button.backgroundColor=[UIColor yellowColor];
            i=2;
       }
        if (i==2) {
            first.backgroundColor=[UIColor yellowColor];
            second.backgroundColor=[UIColor yellowColor];
            third.backgroundColor=[UIColor blackColor];
            button.backgroundColor=[UIColor yellowColor];
            i=3;
        }
       if (i==3) {
            first.backgroundColor=[UIColor yellowColor];
            second.backgroundColor=[UIColor yellowColor];
           third.backgroundColor=[UIColor yellowColor];
            button.backgroundColor=[UIColor blackColor];
            i=0;
        }
    }

首先制作一个全局变量来跟踪哪个按钮的背景发生了变化,还有一个存储所有按钮的数组。

@interface ClassName() {
    int index = 0;
    NSArray *buttons;
}

现在在初始化方法中,这样做

[first setBackgroundColor:[UIColor yellowColor]];
[second setBackgroundColor:[UIColor yellowColor]];
[third setBackgroundColor:[UIColor yellowColor]];
[button setBackgroundColor:[UIColor yellowColor]];
buttons = @[first, second, third, button];
[self performSelector:@selector(changeColor) withObject:nil afterDelay:1.0f];

现在在你的 changeColor 方法中,这样做

- (void)changeColor {

    UIButton *btn = [buttons objectAtIndex:index];
    [btn setBackgroundColor:[UIColor blackColor]];

    UIButton *prevBtn = [buttons objectAtIndex:((index - 1) + 4) % 4];
    [prevBtn setBackgroundColor:[UIColor yellowColor]];

    index = ++ index % 4;
    [self performSelector:@selector(changeColor) withObject:nil afterDelay:1.0f];
}