我正在制作iPhone游戏,让用户选择他们想要的汽车在游戏中的颜色。为了显示哪些车可以选择我有UIButtons与背景图像是各种车。为了显示当前选择的汽车,我在当前汽车颜色后面有一个按钮,背景颜色为黄色。我想要发生的是,当你点击一个汽车按钮时,黄色按钮会移动到被点击的按钮后面。我的代码如下:
-(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;正在执行,但当我看到正在显示的内容时,没有任何变化。
答案 0 :(得分:1)
您应该更改按钮的frame
而不是bounds
。视图的bounds
描述了视图在其自己的坐标空间中的位置和大小。 frame
是超视图坐标空间中的大小和位置。
if (button.backgroundColor == [UIColor yellowColor]) {
button.frame = frame;
}