我有个问题。我想在循环中为一些按钮设置动画。如果序列不重复,它会起作用,但是当序列在数组中重复时我会遇到问题。
- (void)flash:(id)sender delay:(int)time;
{
UIButton *button = (UIButton *)sender;
NSTimeInterval duration = 1;
[UIView animateWithDuration:duration
delay:time + 0.5
options:UIViewAnimationOptionAutoreverse
animations:^{
[button setAlpha:0.1f];
} completion:^(BOOL finished) {
[button setAlpha:1.0f];
}
];
}
(此方法成功,我将方法称为“flash”)
- (void) play: (id)sender
{
//buttons is a array that contains buttons =>
//buttons = [right, left, up, down, nil]
//positions is a sequence of the buttons positions
//if buttons positions is [0,1,2,3]
//positions have the random positions of buttons, example:
//positions = [0, 3, 4, 5,8, 7,9]
int randomNumber;
NSNumber *index;
randomNumber = (arc4random() % 4);
[positions addObject: [NSNumber numberWithInteger:randomNumber]];
UIButton *btn;
for(int i=0; i<[positions count]; i++)
{
index = [positions objectAtIndex:i];
btn = [buttons objectAtIndex:[index intValue]];
[self flash:btn delay:i];
}
}
如果位置序列不重复,例如:positions= [1,2,3,0],则问题没有问题。但是如果序列是例如:positions = [0,0,0,1](它重复数字),程序不会显示动画。