我正在制作iPhone游戏,让用户选择他们希望自己的汽车在游戏中使用的颜色。为了显示哪些车可以选择我有UIButtons与各种汽车的背景图像。为了显示当前选择哪辆车,我在背景颜色为黄色的当前汽车颜色后面有一个按钮。我想要发生的是,当你点击一个汽车按钮时,黄色按钮移动到被点击的按钮后面。我的代码看起来像:UIButton不会以编程方式更改边界
-(void)setPlayerCar:(UIButton *)newCar{
//this code is being called when any of the buttons is clicked
NSArray *carFiles = [NSArray arrayWithObjects:@"redCar.png",@"blueCar.png",@"greenCar.png",@"purpleCar.png",@"turquioseCar.png",@"yellowCar.png", nil];
NSString *file = [carFiles objectAtIndex:newCar.tag];
currentCarImage = file;
CGRect frame;
if(currentCarImage == @"redCar.png"){
frame = CGRectMake(48, 78, 30, 30);
}
if(currentCarImage == @"blueCar.png"){
frame = CGRectMake(83, 78, 30, 30);
}
if(currentCarImage == @"greenCar.png"){
frame = CGRectMake(118, 78, 30, 30);
}
if(currentCarImage == @"purpleCar.png"){
frame = CGRectMake(153, 78, 30, 30);
}
if(currentCarImage == @"turquioseCar.png"){
frame = CGRectMake(188, 78, 30, 30);
}
if(currentCarImage == @"yellowCar.png"){
frame = CGRectMake(223, 78, 30, 30);
}
for(UIButton *button in self.pauseScroll.subviews){
if(button.backgroundColor == [UIColor yellowColor]){
button.bounds = frame;
}
if(button.tag == newCar.tag){
[self.pauseScroll bringSubviewToFront:button];
}
}
}
据我所知,这应该将黄色按钮移动到被选中的按钮。问题是这不会发生,当我调试时我发现正确的按钮被识别,并且该框架被赋予正确的值,并且该行:button.bounds = frame;正在执行,但当我看看什么是显示什么都没有改变。
'currentCarImage == @ “redCar.png”'这不是你如何比较字符串你需要'[currentCarImage isEqualToString:@ “redCar.png”]' – 2011-12-30 00:00:12
我也会考虑改变'if'声明到'else if'语句,这样你就不会每次都执行每个测试。此外,'carFiles'数组可能会更好地放置在其他地方最有可能在法老 – 2011-12-30 01:31:51