2014-03-07 53 views
0

不使用Cocos2D或其他框架,我想让一些UIButton按顺序闪烁,每个按钮间隔1秒。但只有第一个闪烁!任何建议将不胜感激。如何使按钮以编程方式按顺序闪烁

-(void)pressTimerButton{ 

timer = [NSTimer scheduledTimerWithTimeInterval:1.0 
             target:self selector:@selector(timerFires) 
             userInfo:nil repeats:YES]; 

[timer fire]; 
} 

- (void) timerFires{ 

ComputerStringsArray_A=[[NSMutableArray alloc]init]; 
[ComputerStringsArray_A addObject:@"1"]; 
[ComputerStringsArray_A addObject:@"2"]; 
[ComputerStringsArray_A addObject:@"3"]; 
[ComputerStringsArray_A addObject:@"4"]; 
[ComputerStringsArray_A addObject:@"5"]; 

NoElementsInMutableArray = [ComputerStringsArray_A count]; 

NSString *ReadString = ComputerStringsArray_A[increment]; 

if  ([ReadString isEqualToString:one]) {Button = Button_01;} 
else if ([ReadString isEqualToString:two]) {Button = Button_02;} 
else if ([ReadString isEqualToString:three]) {Button = Button_03;} 
else if ([ReadString isEqualToString:four]) {Button = Button_04;} 
else if ([ReadString isEqualToString:five]) {Button = Button_05;} 
else if ([ReadString isEqualToString:six]) {Button = Button_06;} 
else if ([ReadString isEqualToString:seven]) {Button = Button_07;} 
else if ([ReadString isEqualToString:eight]) {Button = Button_08;} 
else if ([ReadString isEqualToString:nine]) {Button = Button_09;} 

[self FlashButton]; 

increment++; 

//Don't try to flash a button beyond the length of the array 
if (increment > NoElementsInMutableArray-1) 
{ 
[timer invalidate]; 
timer = nil; 
} 
} 

-(void) FlashButton 
//- (void)FlashButton:(id)anElement 
{ 
self.Button.alpha = 1.0f; 
[UIView animateWithDuration:0.18 
         delay:0.7 
        options: 
UIViewAnimationOptionCurveEaseInOut | 
//UIViewAnimationOptionRepeat | 
UIViewAnimationOptionAutoreverse | 
UIViewAnimationOptionAllowUserInteraction 

       animations:^{ 
        self.Button.alpha = 0.02f; 
       } 
       completion:^(BOOL finished){ 

        //At the end of the flashing return opacity to 100% and color to white 
        self.Button.alpha = 1.0f; 
        Button.backgroundColor = [UIColor whiteColor]; 
       }]; 
} 
+0

你是否想要像某些线性移动一样闪烁? –

+0

我希望他们一个接一个闪烁:1..2..3..4..5..etc。当下面的按钮闪烁时,上一个按钮已经停止闪烁 – jeddi

回答

1

这会帮助您解决问题吗?

- (void) animateButtonSequence { 

    NSString *ReadString = ComputerStringsArray_A[increment]; 

    UIButton * buttonToFlash; 

    if  ([ReadString isEqualToString:one]) {buttonToFlash = Button_01;} 
    else if ([ReadString isEqualToString:two]) {buttonToFlash = Button_02;} 
    else if ([ReadString isEqualToString:three]) {buttonToFlash = Button_03;} 
    else if ([ReadString isEqualToString:four]) {buttonToFlash = Button_04;} 
    else if ([ReadString isEqualToString:five]) {buttonToFlash = Button_05;} 
    else if ([ReadString isEqualToString:six]) {buttonToFlash = Button_06;} 
    else if ([ReadString isEqualToString:seven]) {buttonToFlash = Button_07;} 
    else if ([ReadString isEqualToString:eight]) {buttonToFlash = Button_08;} 
    else if ([ReadString isEqualToString:nine]) {buttonToFlash = Button_09;} 

    // Perhaps easier to just have an array of your buttons? 
    // All the above code would just be 

    UIButton * buttonToFlash = btnsArray[increment]; 

    [UIView animateWithDuration:0.15 
          delay:0.0 
         options:UIViewAnimationOptionCurveLinear 
        animations:^{ 

         // button flash animation 

        } completion:^(BOOL finished) { 
         if (finished) { 
          increment = increment + 1; 
          if (increment == ComputerStringsArray_A.count) { 
           increment = 0; 

           // done animating, proceed here 

          } 
          else { 
           [self performSelector:@selector(animateButtonSequence) withObject:self afterDelay:1.0]; 
          } 
         } 
        }]; 
} 

这样你就不必跟踪你的计时器。

+0

听起来好像它可能是要走的路。什么时候以及如何定义currentAnimationBlockIndex?干杯 – jeddi

+0

对不起,这是为了'增量',我注意到你在原始问题中使用该变量。 – Logan

+0

宾果!这工作。非常感谢! – jeddi

0

我会建议有一个数组中的所有按钮,而不是尝试执行字符串比较来作出决定。例如:

buttonArray = [[NSMutableArray alloc] initWithObjects:button_01,button_02,button_03,button_04,button_05,button_06,button_07,button_08,button_09, nil]; 

然后在启动计时器时启动一个整数计数器(声明为类的实例变量)。你的timerFires代码变得更简单了。

- (void) timerFires{ 
    Button = [buttonArray objectAtIndex:counter]; 
    [self flashButton]; 
    counter++; 
    if(counter == buttonArray.count){ 
     [timer invalidate]; 
     timer = nil; 
    } 
} 

在这个过程中,你不会失去引用您的按钮,并且不采取任何额外的内存。

编辑:好吧,这是我的项目中的一个类似的功能的实现。代码绝对有效 - 我只是检查它。

- (void) animateKeyboardButton{ 
    if(counter>0) 
     ((UIButton*)[keyboard objectAtIndex:counter - 1]).alpha = 1.0f; 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.3]; 
    if(counter <keyboard.count) 
     ((UIButton*) [keyboard objectAtIndex:counter]).alpha = 0.2f; 
    [UIView commitAnimations]; 
    counter ++; 
    if(counter == keyboard.count+1){ 
     [timer invalidate]; 
     timer = nil; 
     counter = 0; 
    } 
} 
- (void) animateKeyboardWithInterval: (float) interval{ 
    counter = 0; 
    timer = [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(animateKeyboardButton) userInfo:nil repeats:YES]; 
    [timer fire]; 
} 

这是我的一个项目的代码,它有一个28键键盘,存储在NSMutableArray对象中。动画在这里完美运作。希望这可以帮助。

+0

谢谢,但它不能解决问题。但是,只有序列的第一个按钮闪烁。我想按顺序闪烁它们:1..2..3..4..5..etc – jeddi

+0

也许替换[self flashButton]; [self performSelectorOnMainThread:@selector(flashButton)withObject:nil waitUntilDone:YES]; - 也许它试图改变非主线程的UI元素 – Tcharni

+0

完成,但不幸的是,仍然只有第一个闪烁.. – jeddi