ios - How do i make my picture relocate it self on the screen with a button -


im learning xcode , wondering how make button refresh part of code or something. im placing picture in random location on screen want able press button relocate random location, input appreciated. thanks. code. viewcontroller.m

- (void)viewdidload { int xvalue = arc4random() % 320; int yvalue = arc4random() % 480;   uiimageview *imgview = [[uiimageview alloc] initwithframe:cgrectmake(xvalue, yvalue, 70, 30)]; nsstring *imgfilepath = [[nsbundle mainbundle] pathforresource:@"clubs-2-150" oftype:@"jpg"]; uiimage *img = [[uiimage alloc] initwithcontentsoffile:imgfilepath]; [imgview setimage:img]; [self.view addsubview:imgview];   [super viewdidload];    } 

as code stands right not possible variables have scope, means, exists within functions or classes. variable imgview exists on viewdidload. able access somewhere else need declare instance variable.

on .h file this:

@interface yourclassname : uiviewcontroller {    uiimage *imageiwanttochange; }  

this let access imageiwanttochange in of yourclassname functions.

now, on .m

- (void)viewdidload {     [super viewdidload];        int xvalue = arc4random() % 320;    int yvalue = arc4random() % 480;     //notice not have uiimage before imageiwanttochange    imageiwanttochange = [[uiimageview alloc] initwithframe:cgrectmake(xvalue, yvalue, 70, 30)];    uiimage *img = [uiimage imagenamed:@"clubs-2-150.jpg"];    [imageiwanttochange setimage:img];    [self.view addsubview:imgview];  } 

then in ibaction or button selector:

-(ibaction) buttonwaspressed:(id) sender {    cgrect frame = imageiwanttochange.frame;     frame.origin.x = arc4random() % 320;    frame.origin.y = arc4random() % 480;     imageiwanttochange.frame = frame; } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -