iphone - Moving an object randomly around the screen -
i'm trying animate uibutton move randomly around screen in different directions. code below kind of working. button begin moving along random path, however, continues move , forth between point , point b.
[uiview beginanimations:nil context:nil]; [uiview setanimationduration:1]; [uiview setanimationrepeatcount:1000]; [uiview setanimationrepeatautoreverses:yes]; cgfloat x = (cgfloat) (arc4random() % (int) self.view.bounds.size.width); cgfloat y = (cgfloat) (arc4random() % (int) self.view.bounds.size.height); cgpoint squarepostion = cgpointmake(x, y); button.center = squarepostion; [uiview commitanimations];
how can keep moving new random point every time changes directions, instead of moving , forth?
thanks!
try this:
-(void)animationloop:(nsstring *)animationid finished:(nsnumber *)finished context:(void *)context { [uiview beginanimations:nil context:nil]; [uiview setanimationduration:1]; // remove: // [uiview setanimationrepeatcount:1000]; // [uiview setanimationrepeatautoreverses:yes]; cgfloat x = (cgfloat) (arc4random() % (int) self.view.bounds.size.width); cgfloat y = (cgfloat) (arc4random() % (int) self.view.bounds.size.height); cgpoint squarepostion = cgpointmake(x, y); button.center = squarepostion; // add: [uiview setanimationdelegate:self]; // suggested @carl veazey in comment [uiview setanimationdidstopselector:@selector(animationloop:finished:context:)]; [uiview commitanimations]; }
and add counter (int) inside method check if it's executed more 1000 times, if wanna stop it...
Comments
Post a Comment