gpt4 book ai didi

ios - 以编程方式生成按钮,而不会在 Objective-C xcode 中重叠

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:05:23 25 4
gpt4 key购买 nike

我花了几个小时试图完成我认为是一项简单的任务。我正在尝试以编程方式在我的 iPhone 屏幕上生成 10 个未旋转的方形按钮,但它们不会重叠。每当我得到我认为正确的代码时,应用程序就会挂起。到目前为止,这是我正在尝试做的事情:

首先,我有一个 while 循环,不断尝试生成按钮,直到有 10 个(因此挂起)。此循环计算随机宽度和高度以生成按钮。

然后我尝试检查该按钮是否会导致与当前放置在屏幕上的任何按钮重叠(注意:这是为了提高效率,但已经过测试)。如果按钮可能重叠,我将“继续;”,但如果不是,则生成按钮。

生成按钮后,我将不应放置其他按钮的坐标放入其特定的、基于轴的数组中,以检查前面的循环。

这是我的代码:

- (void)addNumbers{
int width = [[UIScreen mainScreen] bounds].size.width - 70;
int height = [[UIScreen mainScreen] bounds].size.height - 110;
NSMutableArray* xarray = [[NSMutableArray alloc] init];
NSMutableArray* yarray = [[NSMutableArray alloc] init];
int buttons = 0;
while(buttons < 10) {
int x = 10*floor(10+arc4random_uniform(width)/10);
int y = 10*floor(50+arc4random_uniform(height)/10);
NSLog(@"The coords are: %i, %i",x,y);
if([xarray containsObject:[NSNumber numberWithInt:x]] && [yarray containsObject:[NSNumber numberWithInt:y]]){
NSLog(@"Triggered");
continue;
}
[self generateButton:x :y :buttons];
buttons++;
for(int i = 0; i < 6; i++){
[xarray addObject:[NSNumber numberWithInt:(x+i*10)]];
[yarray addObject:[NSNumber numberWithInt:(y+i*10)]];
[xarray addObject:[NSNumber numberWithInt:(x-i*10)]];
[yarray addObject:[NSNumber numberWithInt:(y-i*10)]];
}
}
}

- (void)generateButton:(int)x :(int)y :(int)num{
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(x, y, 60, 60);
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor whiteColor]];
btn.titleLabel.font = [UIFont fontWithName:@"Avenir" size:40.0f];
[btn setTitle:[NSString stringWithFormat:@"%d",num] forState:UIControlStateNormal];
[self.view addSubview:btn];
[self.squareArray addObject:btn];
}

请帮帮我:'(

另外,我是 Objective-C 的新手(可能很明显),所以请随时评论我可以改进我的代码或效率的任何方式。

最佳答案

我会给你我会做的事。

-(BOOL)isButtonOverlapping:(NSArray *)array button:(UIButton *)btn {
for (UIButton *btn_ in [array copy]) {
if (CGRectIntersectsRect(btn_, btn)) return YES;
}
return NO;
}

-(void)addNumbers {
int width = [[UIScreen mainScreen] bounds].size.width - 70;
int height = [[UIScreen mainScreen] bounds].size.height - 110;

NSMutableArray *buttonsArray = [NSMutableArray new];

for (short button = 0; button < 10; button++) {
UIButton *btn = [self generateButton:x :y :button];

do {
btn.center = CGPointMake(rand() % width, rand() % height);
} while ([self isButtonOverlapping:buttonsArray button:btn]);

[buttonsArray addObject:btn];
}
}

-(UIButton *)generateButton:(int)x :(int)y :(int)num {
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(x, y, 60, 60);
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor whiteColor]];
btn.titleLabel.font = [UIFont fontWithName:@"Avenir" size:40.0f];
[btn setTitle:[NSString stringWithFormat:@"%d",num] forState:UIControlStateNormal];
[self.view addSubview:btn];
[self.squareArray addObject:btn];

return btn;
}

关于ios - 以编程方式生成按钮,而不会在 Objective-C xcode 中重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23965532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com