kb920
kb920

Reputation: 3089

Set Uibutton Random Position on UIView

I want to set 5 buttons on uiview at random position. Buttons need maintain some spacing to each other. I mean buttons should not overlap to each other. All buttons set on UIView come from corners with rotation animation.

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

I Can rotate buttons using above code but can you pls. help me for set buttons on random position with out overlapping by each other. If points are fix than I can set buttons with animation by this code but I want random position of buttons.

    [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];

Thanks in advance.

Upvotes: 0

Views: 1145

Answers (2)

WolfLink
WolfLink

Reputation: 3317

Based on spiritofmysoul.wordpress's code:

//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;
}  

Beware: This will work well for small amounts of buttons on a large space but as you add buttons and you run out of space, this method will take much longer to run. If there is not enough space for all the buttons, it will become an infinite loop.

Upvotes: 1

JohnVanDijk
JohnVanDijk

Reputation: 3581

1st, add buttons to an NSArray, only to make things easier:

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

Now, this code tries to Arrange them at random positions.

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

Implement this function somewhere too:

- (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;

OK this is a workign soln., I hope, just added something to WolfLink's answer. Check This.

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

Upvotes: 3

Related Questions