0

我有个问题。我想在循环中为一些按钮设置动画。如果序列不重复,它会起作用,但是当序列在数组中重复时我会遇到问题。

- (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](它重复数字),程序不会显示动画。

4

2 回答 2

1

此方法将为您传递给它的视图设置动画。

-(void)flashingAnimation:(BOOL)boolVal forView:(UIView *) view{
[UIView animateWithDuration:0.5
                      delay:0.0
                    options:UIViewAnimationOptionRepeat | 
 UIViewAnimationOptionAutoreverse
                 animations:^{
                     view.alpha = 0.0f;
                     view.frame = CGRectMake(view.frame.origin.x*0.75, view.frame.origin.y*0.75, view.frame.size.width*1.5, view.frame.size.height*1.5);
                 }   
                    completion:^(BOOL finished){
                     // Do nothing
                 }];
[UIView commitAnimations];
}

method最后通过传递View你想要的来调用上面的内容animate

[self flashingAnimation:YES forView:Btn]

在选项中添加UIViewAnimationOptionRepeat

于 2013-07-29T15:14:57.857 回答
1

它不起作用,因为当您调用flash并且您发送的按钮与已经动画的按钮相同时,动画被中断(如果由于延迟而尚未真正开始)。

尝试重构以单独跟踪时间并立即使用动画。也许一种解决方案是递归调用自身的完成块。

于 2013-07-29T15:18:54.453 回答