在任意位置使用按钮填充视图 [英] Fill view with buttons at random positions

查看:43
本文介绍了在任意位置使用按钮填充视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用按钮( UIButton )填充区域( UIView ),以使它们彼此不相交.

i want to fill area (UIView) with buttons(UIButton) so that they do not intersect with each others.

我的想法:

  • 在视图中的随机位置创建初始按钮;
  • 使用初始按钮(计数<20)中的其他按钮彼此不相交〜10像素填充视图.

到目前为止我做了什么:

What have i done so far:

我创建了方法:

-(void)generateButtonsForView:(UIView *)view buttonCount:(int)count
{
//get size of main view
    float viewWidth = view.frame.size.width;
    float viewHeight = view.frame.size.height;

    //set button at random position
    UIButton *initialButton = [[UIButton alloc] initWithFrame:CGRectMake(arc4random() % (int)viewWidth,
                                                                         arc4random() % (int)viewHeight,
                                                                         buttonWidth, buttonHeight)];

    [initialButton setBackgroundImage:[UIImage imageNamed:@"button"] forState:UIControlStateNormal];

    [view addSubview:initialButton];

    // set count to 20 - max number of buttons on screen
    if (count > 20)
        count = 20;

    //fill view with buttons from initial button +- 10 pixels
    for (int i=0;i<=count;i++)
    {
        //button
        UIButton *otherButtons = [[UIButton alloc] init];
        [otherButtons setBackgroundImage:[UIImage imageNamed:@"button"] forState:UIControlStateNormal];

        ...//have no idea what to do here

    }
}

因此,我对要根据初始按钮生成其他按钮位置的位置感到困惑.我不知道如何生成它们彼此之间相距5-10像素的位置...任何想法如何实现这一点?谢谢!

So i'm confused on the place where i want to generate the position of the other buttons, depending on the initial button. I do not know how to generate theirs position that they were at a distance of 5-10 pixels from each other... Any ideas how to realise this? Thanks!

推荐答案

这是一个带有视图而不是按钮的示例,但是概念是相同的.我使用CGRectInset为新的潜在视图提供10点的缓冲,然后查看新视图是否与其他任何视图相交.如果没有相交,请添加子视图;如果存在,请使用新的随机位置再试一次.

Here's an example with views rather than buttons, but the concept is the same. I use CGRectInset to give the new potential view a buffer of 10 points around it, then look for whether the new view intersects any of the other views. If there's no intersection, add the subview, if there is, try again with a new random location.

-(void)generateButtonsForView {
    float viewWidth = self.view.frame.size.width;
    float viewHeight = self.view.frame.size.height;

    UIView *initialView = [[UIView alloc] initWithFrame:CGRectMake(arc4random() % (int)viewWidth, arc4random() % (int)viewHeight, 50, 30)];
    initialView.backgroundColor = [UIColor redColor];
    [self.view addSubview:initialView];
    int numViews = 0;
    while (numViews < 19) {
        BOOL goodView = YES;
        UIView *candidateView = [[UIView alloc] initWithFrame:CGRectMake(arc4random() % (int)viewWidth, arc4random() % (int)viewHeight, 50, 30)];
        candidateView.backgroundColor = [UIColor redColor];
        for (UIView *placedView in self.view.subviews) {
            if (CGRectIntersectsRect(CGRectInset(candidateView.frame, -10, -10), placedView.frame)) {
                goodView = NO;
                break;
            }
        }
        if (goodView) {
            [self.view addSubview:candidateView];
            numViews += 1;
        }
    }
}

这篇关于在任意位置使用按钮填充视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆