objective c - How to align components to grid? -
objective c - How to align components to grid? -
i'm working on application users can set own controls on view. i'm working on details want controls aligned sort of grid.
how can draw grid? how can create sure controls aligned automatically grid (to closest corner?)thanks in advance!
anyone can help?
for still interested in answer:
i first drew horizontal , vertical line:
-(void)drawgrid {
for(int y=self.gridsize;y<self.templateeditview.frame.size.height;y+=self.gridsize){ uiview *hline = [[uiview alloc] init]; [hline setbackgroundcolor:[[uicolor blackcolor] colorwithalphacomponent:0.1f]]; hline.frame = cgrectmake(0, y, self.templateeditview.frame.size.width, 1); [self.templateeditview addsubview:hline]; } for(int x=self.gridsize;x<self.templateeditview.frame.size.width;x+=self.gridsize){ uiview *vline = [[uiview alloc] init]; [vline setbackgroundcolor:[[uicolor blackcolor] colorwithalphacomponent:0.1f]]; vline.frame = cgrectmake(x, 0, 1, self.templateeditview.frame.size.height); [self.templateeditview addsubview:vline]; }
}
when 1 of objects dragged , dropped, have align grid:
-(void)aligntogrid:(uiview *)control{
int gridsize = self.gridsize; int oldx = (int)control.frame.origin.x; int newx = (oldx /gridsize)*gridsize; if(oldx%gridsize > gridsize/2){ newx+=gridsize; } int oldy = (int)control.frame.origin.y; int newy = (oldy /gridsize)*gridsize; if(oldy%gridsize > gridsize/2){ newy+=gridsize; } [uiview animatewithduration:0.3f delay:0.0f options:uiviewanimationoptioncurvelinear animations:^ { [control setframe: cgrectmake(newx,newy,control.frame.size.width,control.frame.size.height)]; } completion:nil];
}
objective-c grid alignment
Comments
Post a Comment