Create UIButtons random position without intersection

I have 6 uibuttons in an array. And i want to set random positions for them without intersection. I tried a few things and my project works fine. But when i force the app to generate new buttons, it stucks without an error.

for (UIButton *button in arr) {

    float widthOffset = self.gameView.frame.size.width-button.frame.size.width;
    float heightOffset = self.gameView.frame.size.height-button.frame.size.height;

    button.frame = CGRectMake(arc4random()%(int)widthOffset, arc4random()%(int)heightOffset, button.frame.size.width, button.frame.size.height);

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


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

In my opinion, the problem is in while part. But i can't figure out.

Any advice is appreciated.

Best Regards, Taha

Answers


Need Your Help

How do i generate a fresh access token from a stale token in django-allauth?

python django facebook django-allauth

I have integrated allauth in my django project and users are able to log in via their facebook accounts. Now their tokens are available in the database but when i use them to get data of the users ...

FxCop on build (Visual Studio 2008 Professional)

visual-studio msbuild fxcop

I just learned how to integrate StyleCop into Visual Studio. Now it runs every build and its errors appears as warnings. Excelent!

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.