iphone - Drag an UIImageView in a restricted range -


how possible drag uiimageview within area of screen? code looks this:

- (void) touchesmoved:(nsset *)touches withevent:(uievent *)event{     uitouch *touch = [[event alltouches] anyobject];     touch.view.frame = cgrectmake(104, 171, 113, 49);      if([touch view] == toggle)     {          cgpoint location = [touch locationinview:self.view];         cgpoint newlocation = cgpointmake(toggle.center.x, location.y);          toggle.center = newlocation;         nslog(@"%f \n",toggle.center.y);      }   } 

i want able drag image within frame have defined.

you can use cgrectcontainsrect(rect1, rect2) check if first rect inside of second

bool cgrectcontainsrect (    cgrect rect1,    cgrect rect2 ); 

when using uiviews , want see if 1 view falls within frame of second, related function cgrectcontainsrect checking you. not check intersection; union of both rectangles must equal first rectangle return true. function takes 2 arguments. first rectangle surrounding item. second argument either falls inside first or not.

so code this

cgpoint location = [touch locationinview:self.view]; cgpoint newlocation = cgpointmake(toggle.center.x, location.y);  cgrect r = cgrectmake(newlocation.x-self.frame.size.width/2,                        newlocation.y-self.frame.size.height/2,                        self.frame.size.width,                       self.frame.size.height); if(cgrectcontainsrect(r, theotherrect)){     toggle.center = newlocation;     nslog(@"%f \n",toggle.center.y); } 

other useful coregraphics functions: http://blogs.oreilly.com/iphone/2008/12/useful-core-graphics-functions.html

another hint:nslog(@"%@", nsstringfromcgpoint(toggle.center)) makes logging of cgtypes easier. use equivalently: nsstringfromcgrect(rect)


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -