【问题标题】:Set Uibutton Random Position on UIView在 UIView 上设置 Uibutton 随机位置
【发布时间】:2013-07-11 05:53:00
【问题描述】:

我想在 uiview 上随机设置 5 个按钮。按钮之间需要保持一定的间距。我的意思是按钮不应该相互重叠。 在 UIView 上设置的所有按钮都来自带有旋转动画的角落。

        btn1.transform =    CGAffineTransformMakeRotation(40);
        btn2.transform = CGAffineTransformMakeRotation(60);
        btn3.transform = CGAffineTransformMakeRotation(90);
        btn4.transform = CGAffineTransformMakeRotation(30);
        btn5.transform = CGAffineTransformMakeRotation(20);

我可以使用上面的代码来旋转按钮,但是你可以吗?帮助我在随机位置设置按钮,而不会相互重叠。 如果点是固定的,我可以通过此代码设置带有动画的按钮,但我想要按钮的随机位置。

    [AnimationView moveBubble:CGPointMake(18, 142) duration:1 : btn1];
    [AnimationView moveBubble:CGPointMake(118, 142) duration:1 : btn2];
    [AnimationView moveBubble:CGPointMake(193, 142) duration:1 : btn3];
    [AnimationView moveBubble:CGPointMake(18, 216) duration:1 : btn4];

提前致谢。

【问题讨论】:

标签: iphone ios objective-c


【解决方案1】:

第一,向 NSArray 添加按钮,只是为了让事情变得更简单:

NSArray *buttonArray = @[btn1,btn2,btn3,btn4,btn5];

现在,这段代码尝试将它们排列在随机位置。

int xTemp, yTemp;
for (int i = 0; i < 5; i++) {
    while (YES) {
        xTemp = arc4random_uniform(view.frame.size.width - [buttonArray[i] frame].size.width);
        yTemp = arc4random_uniform(view.frame.size.height - [buttonArray[i] frame].size.height);
        if (![self positionx:xTemp y:yTemp intersectsAnyButtonTillIndex:i inButtonArray:buttonArray]) {
            [AnimationView moveBubble:CGPointMake(xTemp, yTemp) duration:1 : buttonArray[i]];
            break;
        }
    }
}

也可以在某处实现这个功能:

- (BOOL) positionx:(int)xTemp y:(int)yTemp intersectsAnyButtonTillIndex:(int)index inButtonArray:(NSArray *)buttonArray {
    //Again please change the < to <= , I'm sorry, doing too many things at once.
for (int i = 0; i <= index; i++) {
    CGRect frame = [buttonArray[i] frame];
    //EDIT : In the logic earlier, I had wrongly done a minus where I should have done a plus.
    if ((xTemp > frame.origin.x && xTemp < (frame.size.width + frame.origin.x)) && (yTemp > frame.origin.y && yTemp < (frame.size.height + frame.origin.y))) return YES;
    }
    return NO;

好的,这是一个工作解决方案。我希望,只是在 WolfLink 的答案中添加了一些内容。检查这个。

for (UIButton *button in buttonArray) {
    button.frame = CGRectMake(arc4random_uniform(view.frame.size.width - button.frame.size.width), arc4random_uniform(view.frame.size.height - button.frame.size.height), button.frame.size.width, button.frame.size.height);
    while ([self button:button intersectsButtonInArray:buttonArray]) {
        button.frame = CGRectMake(arc4random_uniform(view.frame.size.width - button.frame.size.width), arc4random_uniform(view.frame.size.height - button.frame.size.height), button.frame.size.width, button.frame.size.height);
    }

    //another function
    -(BOOL)button:(UIButton *)button intersectsButtonInArray:(NSArray *)array {
        for (UIButton *testButton in array) {
            if (CGRectIntersectsRect(button.frame, testButton.frame) && ![button isEqual:testButton]) {
                return YES;
            }
        }
        return NO;
    }

【讨论】:

  • :您的代码运行良好,但按钮有时会相互重叠。
  • 仍然面临相同的prb ...:(
  • 为什么我只看到 4 个按钮?您只有 4 个按钮还是 5 个?如果您对代码进行了更改,我可以看到吗?
  • 很高兴能帮上忙。 :)
【解决方案2】:

基于spiritofmysoul.wordpress的代码:

//in the function where you randomize the buttons
NSArray *buttonArray = @[btn1,btn2,btn3,btn4,btn5];
    for (UIButton *button in buttonArray) {
        float widthOffset = self.frame.size.width-button.frame.size.width;
        float heightOffset = self.frame.size.height-button.frame.size.height;

        button.frame = CGRectMake(arc4random()%widthOffset, arc4random()%heightOffset, button.frame.size.width, button.frame.size.height);
        while ([self button:button intersectsButtonInArray:buttonArray]) {
            button.frame = CGRectMake(arc4random(), arc4random(), button.frame.size.width, button.frame.size.height);
        }

//another function
-(BOOL)button:(UIButton *)button intersectsButtonInArray:(NSArray *)array {
    for (UIButton *testButton in array) {
        if (CGRectIntersectsRect(button.frame, testButton.frame) && ![button isEqual:testButton]) {
            return YES;
        }
    }
    return NO;
}  

注意:这适用于大空间上的少量按钮,但是当您添加按钮并且空间不足时,此方法将需要更长的时间来运行。如果所有按钮都没有足够的空间,就会变成无限循环。

【讨论】:

  • 嘿 WolfLink,有一个问题,如果原点的 x 或 y 太靠近屏幕的右端或底部,您的按钮可能会部分位于屏幕之外。 :)
  • @WolfLink:按钮重叠问题解决了,但有些时候按钮超出了查看范围。在uiview中不是不可见的
  • @WolfLink: 按钮有时会离开屏幕,所以它不可见。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多