ios - How can I make a UILabel change positions randomly? -
what mean is, if have uilabel created, how can create alter periodically within view of screen random point? utilize arc4random, or random cgpointmake?
i have alter every sec or using this
text = [nstimer scheduledtimerwithtimeinterval:1 target:self selector:@selector(randomtext) userinfo:nil repeats:yes] ;
but i'm not sure how "spawn" in random place, if possible, , remove other one.
a simpler version be, if made say, 4 different uilabels, how create appear randomly on 1 of 4, , remove after time interval? assume utilize like
int randomnumber = rand() % 4;
and set switch in there, i'm not sure if right way either.
do have single label want move different points on screen? or adding multiple labels?
are using auto-layout or not? (if you're using auto-layout can't move label around - instead need modify it's position constraints. gets little more complicated.)
as how move around, suggest using arc4random_uniform calculate new location label's center.
you set code next timer method. in code below, "view" view contains label.
the code below keeps label visible on-screen moving center no closer edges of it's parent view half label's width&height.
cgfloat newx = arc4random_uniform(view.bounds.width - label.bounds.width) + label.bounds.width/2; cgfloat newy = arc4random_uniform(view.bounds.height - label.bounds.height) + label.bounds.height/2; label.center = cgpointmake(newx, newy);
if want label move it's new location in animation utilize uiview method animatewithduration:animations alter center point in animation block.
again, if you're using autolayout, you'll need attach center x , center y constraints outlets, , alter value settings move label.
ios objective-c random uilabel viewcontroller
No comments:
Post a Comment