ios - Fill view with buttons at random positions -


i want fill area (uiview) buttons(uibutton) not intersect each others.

my idea:

  • create initial button @ random position in view;
  • fill view other buttons initial button (count < 20) not intersect ~10 pixels each other.

what have done far:

i created method:

-(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 @ 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 20 - max number of buttons on screen     if (count > 20)         count = 20;      //fill view buttons initial button +- 10 pixels     (int i=0;i<=count;i++)     {         //button         uibutton *otherbuttons = [[uibutton alloc] init];         [otherbuttons setbackgroundimage:[uiimage imagenamed:@"button"] forstate:uicontrolstatenormal];          ...//have no idea here      } } 

so i'm confused on place want generate position of other buttons, depending on initial button. not know how generate theirs position @ distance of 5-10 pixels each other... ideas how realise this? thanks!

here's example views rather buttons, concept same. use cgrectinset give new potential view buffer of 10 points around it, whether new view intersects of other views. if there's no intersection, add subview, if there is, try again 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];         (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;         }     } } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -