objective c - how to make an object move by touching it in iOS -
i newbie in objective-c , trying make simple app in when touch object move randomly while , stops. need touch again move again , stop after while.
i have searched touch method , tutorials, problem don't know how start. need function move object , 1 touch it, don't know how connect them , use them.
here tutorial helped me lot view of functionality , function in opposite way of app. still can not start programming on own.
http://xcodenoobies.blogspot.se/2010/11/under-construction.html
any appreciated, regarding how program logic , how find right methods , how find type of variables need.
step 1: put code in viewdidload
method, in have created uiimageview
, add view randomly
[self.view settag:1]; for(int i=0;i<4;i++) { int x = arc4random()%300; int y = arc4random()%400; #warning set image here uiimageview *imgview = [[uiimageview alloc] initwithimage:[uiimage imagenamed:@"someimage.png"]]; [imgview setframe:cgrectmake(x, y, 25, 25)]; [imgview setuserinteractionenabled:yes]; [self.view addsubview:imgview]; }
step 2 : define touchbegan
method handle touch , move objects around view, have set tag = 1 viewcontroller
,because dont want move our mainview, subviews moved
- (void)touchesbegan:(nsset *)touches withevent:(uievent *)event { uitouch *touch = [touches anyobject]; if([[touch view] tag] != 1) { [uiview animatewithduration:0.25f animations:^{ int x = arc4random()%300; int y = arc4random()%400; [[touch view] setcenter:cgpointmake(x, y)]; }]; } }