0

我想在 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];

提前致谢。

4

2 回答 2

3

第一,将按钮添加到 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;
    }
于 2013-07-11T06:33:46.827 回答
1

基于 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;
}  

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

于 2013-07-11T07:12:58.787 回答