【发布时间】:2014-08-14 09:50:33
【问题描述】:
如何仅将 4 个 UIButtons 放置在 4 个随机位置。 我有一个应用程序,每次按下更改位置按钮时都有 4 个 UIButtons,UIButtons 应该改变那里的位置,例如 Button1 随机移动到 Button4 的位置,Button4 随机移动到任何其他 Button 的位置
当我使用此代码时,按钮位置没有变化
-(void)changeButtonPosition {
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSValue valueWithCGRect:CGRectMake(222,246,27,21)]];
[array addObject:[NSValue valueWithCGRect:CGRectMake(80, 246, 27, 21)]];
[array addObject:[NSValue valueWithCGRect:CGRectMake(165, 324, 27, 21)]];
[array addObject:[NSValue valueWithCGRect:CGRectMake(222, 324, 27, 21)]];
NSUInteger count = [array count];
for (NSUInteger i = 0; i < count; ++i) {
int nElements = count - i;
int n = (arc4random() % nElements) + i;
[array exchangeObjectAtIndex:i withObjectAtIndex:n];
}
btn1.frame = [((NSValue *)[array objectAtIndex:0]) CGRectValue];
btn2.frame = [((NSValue *)[array objectAtIndex:1]) CGRectValue];
btn3.frame = [((NSValue *)[array objectAtIndex:2]) CGRectValue];
btn4.frame = [((NSValue *)[array objectAtIndex:3]) CGRectValue];
} 谢谢!
【问题讨论】:
-
你需要写一个函数来改变每个按钮的(x,y),就是这样。自己尝试一下,如果之后需要任何帮助,请发布您尝试过的代码。
-
我需要将按钮移动到随机位置,如果我更改 (x,y),那么该位置将保持在我决定它应该移动到 4 帧中的随机位置的位置
-
嘿,伙计,可以通过将所有帧放入 nasarry 并根据您的选择更改数组并分配给按钮帧
标签: ios objective-c xcode uibutton cgrectmake