不使用Cocos2D或其他框架,我想让一些UIButtons按顺序闪烁,每个UIButtons间隔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 :(得分: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];
}
}
}];
}
这样您就不必跟踪计时器了。
答案 1 :(得分: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对象中。动画在这里完美运作。希望这会有所帮助。