Saturday 15 January 2011

objective c - UIViews Overlap after adding Gravity and Collision behaviour -



objective c - UIViews Overlap after adding Gravity and Collision behaviour -

i trying stack uiviews on click of button. i've added gravity & collision behaviour using uikit dynamics. problem can see image (http://pho.to/7nvji) is, lower greenish block , block on top of seems overlapping. here viewdidload method : `- (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib.

uibutton *moveit = [uibutton buttonwithtype:uibuttontyperoundedrect]; moveit.frame = cgrectmake(200, 50, 70, 30); [moveit addtarget:self action:@selector(buttonclicked:) forcontrolevents:uicontroleventtouchdown]; [self.view addsubview:moveit]; moveit.titlelabel.text = @"hit it"; moveit.titlelabel.textcolor = [uicolor whitecolor]; moveit.backgroundcolor = [uicolor bluecolor]; uibutton *addblockbutton = [uibutton buttonwithtype:uibuttontyperoundedrect]; addblockbutton.frame = cgrectmake(20, 50, 70, 30); [addblockbutton addtarget:self action:@selector(addblock:) forcontrolevents:uicontroleventtouchdown]; [self.view addsubview:addblockbutton]; addblockbutton.backgroundcolor = [uicolor orangecolor]; animator = [[uidynamicanimator alloc] initwithreferenceview:self.view]; newblockycoordiante = self.view.bounds.size.height-blockheight; uiview *basicblock = [[uiview alloc] initwithframe:cgrectmake(64, newblockycoordiante, blockwidth, blockheight)]; basicblock.layer.borderwidth = 1; basicblock.tag = 100; basicblock.layer.bordercolor = [uicolor greencolor].cgcolor; basicblock.backgroundcolor = [uicolor clearcolor]; [self.view addsubview:basicblock]; newblockycoordiante = newblockycoordiante - blockheight; if (collisonbehaviour == nil) { collisonbehaviour = [[uicollisionbehavior alloc] initwithitems:@[basicblock]]; collisonbehaviour.translatesreferenceboundsintoboundary = yes; collisonbehaviour.collisiondelegate = self; [animator addbehavior:collisonbehaviour]; } if (gbehaviour == nil) { gbehaviour = [[uigravitybehavior alloc] initwithitems:@[basicblock]]; [animator addbehavior:gbehaviour]; }

}`

and here how add together new blocks on top of previous blocks :

- (void) addblock :(id)sender { uiview *basicblock = [[uiview alloc] initwithframe:cgrectmake(64, newblockycoordiante, blockwidth, blockheight)]; basicblock.layer.borderwidth = 1; basicblock.layer.bordercolor = [uicolor orangecolor].cgcolor; basicblock.backgroundcolor = [uicolor clearcolor]; [self.view addsubview:basicblock]; newblockycoordiante = newblockycoordiante - blockheight; [gbehaviour additem:basicblock]; [collisonbehaviour additem:basicblock]; }

how ensure these blocks doesn't overlap while stacking up. overlapping causes issue, when add together tower similar next on right , add together force behaviour greenish block move towards right, instead of moving adjacent block sec tower moves sec lastly block 2nd tower. pointers/help appreciated. in advance :).

i believe uidynamics adds little "give" objects , friction when collide , move deed more realistically. may able cut down this, using uidynamicitembehavior , reducing elasticity , bounce, i'm not sure.

long story short uidynamics tends imprecise scheme , if you're looking animate interface items in, i'd recommend using dynamics until items reach desired location , removing behaviors , setting exact locations :)

objective-c uikit-dynamics uicollisionbehavior uigravitybehavior uipushbehavior

No comments:

Post a Comment