如何仅将 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];
} 谢谢 !