在不使用 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];
}];
}
最佳答案
这是否有助于解决您的问题?
- (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];
}
}
}];
}
这样您就不必跟踪计时器。
关于ios - 如何以编程方式使按钮按顺序闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22243109/