我有4 UIButtons
我希望以随机顺序将浅色图像闪烁一秒钟。显然我想以编程方式更改按钮图像。正如您在 XCode Project方法中看到的那样:showSequence,它会尝试将图像设置为较浅的图像,然后再将图像重新设置,但它不会在模拟器上显示。
代码段:
- (void) showSequence:(NSMutableArray *)sequenceArray {
for(int i = 0; i < [sequenceArray count]; i++){
int sequenceNumber = [[sequenceArray objectAtIndex:i] intValue];
switch (sequenceNumber) {
case 1: {
NSLog(@"green");
[self.greenButton setBackgroundImage:[UIImage imageNamed:@"green_active.png"] forState:UIControlStateNormal];
sleep(1);
[self.greenButton setBackgroundImage:[UIImage imageNamed:@"green_inactive.png"] forState:UIControlStateNormal];
break;
}
case 2: {
NSLog(@"red");
[self.redButton setBackgroundImage:[UIImage imageNamed:@"red_active.png"] forState:UIControlStateNormal];
sleep(1);
[self.redButton setBackgroundImage:[UIImage imageNamed:@"red_inactive.png"] forState:UIControlStateNormal];
break;
}
case 3: {
NSLog(@"yellow");
[self.yellowButton setBackgroundImage:[UIImage imageNamed:@"yellow_active.png"] forState:UIControlStateNormal];
sleep(1);
[self.yellowButton setBackgroundImage:[UIImage imageNamed:@"yellow_inactive.png"] forState:UIControlStateNormal];
break;
}
case 4: {
NSLog(@"blue");
[self.blueButton setBackgroundImage:[UIImage imageNamed:@"blue_active.png"] forState:UIControlStateNormal];
sleep(1);
[self.blueButton setBackgroundImage:[UIImage imageNamed:@"blue_inactive.png"] forState:UIControlStateNormal];
break;
}
default:
break;
}
}
}
提前致谢(顺便说一下,在序列中不要点击开始按钮它会破坏(我还在继续工作))
答案 0 :(得分:1)
你可以通过简单地将imageView添加到Button&amp; amp;将NSArray指定给imageView.animationImages属性。 NSArray必须按照您的顺序包含图像 见下面的代码
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"myImageFrame1.gif"],
[UIImage imageNamed:@"myImageFrame2.gif"],
[UIImage imageNamed:@"myImageFrame3.gif"],
[UIImage imageNamed:@"myImageFrame4.gif"], nil];
imageView.animationDuration = 2.0;
imageView.animationRepeatCount = 1; // for showing only once, otherwise set to 0
[imageView startAnimating];
[self.button addsubView:imageView]
答案 1 :(得分:0)
检查
setBackgroundImage:
尝试setImage: