ios - Set Images Randomly for buttons from array and take button's cgrect's from aray -
i have 42 buttons in storyboard , have take array of images. in array there total 7 images.
nsarray *arrimg=[nsarray arraywithobjects:@"book.png", @"lock.png", @"exclamation.png", @"game.png", @"music.png", @"videos.png", @"camera32.png", nil];
so how take random images every time on button?
should take button collection or else?
note:- have not take buttons programatically.
a new question stand: how take 42 button's cgrect array , display randomly in screen.
nsmutablearray *mutarr=[[nsmutablearray alloc]init]; nsmutablearray *array=[nsmutablearray arraywitharray:mutarr]; [array addobject:[nsvalue valuewithcgrect:cgrectmake(50, 50, 47, 25)]]; [array addobject:[nsvalue valuewithcgrect:cgrectmake(97, 50, 47, 25)]]; [array addobject:[nsvalue valuewithcgrect:cgrectmake(144, 50, 47, 25)]]; cgrect somerect1 = [[array objectatindex:0] cgrectvalue]; cgrect somerect2 = [[array objectatindex:0] cgrectvalue]; cgrect somerect3 = [[array objectatindex:0] cgrectvalue];
i don't know ahead.
give tag each 42 button 0 41
- (void)viewdidload { [super viewdidload]; arrimg=[nsarray arraywithobjects:@"book.png", @"lock.png", @"exclamation.png", @"game.png", @"music.png", @"videos.png", @"camera32.png", nil]; nstimer *timer = [nstimer timerwithtimeinterval:1.0f target:self selector:@selector(performaction) userinfo:nil repeats:yes]; [[nsrunloop currentrunloop] addtimer:timer formode:nsrunloopcommonmodes]; } -(void)performaction { nsinteger randomnumberforimage = rand() % (arrimg.count); nsinteger randomnumberforbutton=rand()%(numberofbutton);// numberofbutton=42; uibutton *btn=[self viewwithtag:randomnumberforbutton]; [btn setimage:[uiimage imagenamed:[arrimg objectatindex:randomnumberforimage]] forstate:uicontrolstatenormal]; }
Comments
Post a Comment