• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 10:08:14 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我花了几个小时试图完成我认为很简单的任务。我正在尝试以编程方式在我的 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 numberWithIntx+i*10)]];
            [yarray addObject:[NSNumber numberWithInty+i*10)]];
            [xarray addObject:[NSNumber numberWithIntx-i*10)]];
            [yarray addObject:[NSNumber numberWithInty-i*10)]];
        }
    }
}

- (void)generateButtonint)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 的新手(可能很明显),所以请随意评论我可以改进我的代码或效率的任何方式。



Best Answer-推荐答案


我会给你我会做的。

-(BOOL)isButtonOverlappingNSArray *)array buttonUIButton *)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 *)generateButtonint)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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap