iphone - How to restrict uilabel to be dragged within a particular frame only? -
i have added 1 label in imageview , added pangesture dragging label.with code label dragged within whole.but want restrict label drag within frame of uiimageview only. how can achieve this? code adding lable , gesture below.
lblquote=[[uilabel alloc]init]; lblquote.frame=cgrectmake(130,380, 400,100); lblquote.userinteractionenabled=true; lblquote.backgroundcolor=[uicolor clearcolor]; lblquote.userinteractionenabled=true; lblquote.linebreakmode=uilinebreakmodewordwrap; lblquote.font=[uifont systemfontofsize:40.0]; lblquote.tag=500; lblquote.text=@""; cgsize maximumlabelsize = cgsizemake(296,9999); cgsize expectedlabelsize = [strquote sizewithfont:lblquote.font constrainedtosize:maximumlabelsize linebreakmode:lblquote.linebreakmode]; //adjust label the new height. int nooflines=expectedlabelsize.height/16.0; lblquote.numberoflines=nooflines; // cgrect newframe = lblquote.frame; //newframe.size.height = expectedlabelsize.height; // yourlabel.frame = newframe; lblquote.textalignment=uitextalignmentcenter; uipangesturerecognizer *gesture = [[[uipangesturerecognizer alloc] initwithtarget:self action:@selector(labeldragged:)] autorelease]; [lblquote addgesturerecognizer:gesture]; - (void)labeldragged:(uipangesturerecognizer *)gesture { uilabel *label = (uilabel *)gesture.view; cgpoint translation = [gesture translationinview:label]; // move label label.center = cgpointmake(label.center.x + translation.x, label.center.y + translation.y); // reset translation [gesture settranslation:cgpointzero inview:label]; }
i tried moving label on touches event.code below:
- (void)touchesmoved:(nsset *)touches withevent:(uievent *)event { uitouch *touch = [touches anyobject]; cgpoint pointmoved = [touch locationinview:imgview2]; lblquote.frame=cgrectmake(pointmoved.x, pointmoved.y, lblquote.frame.size.width, lblquote.frame.size.height); }
but movement not smooth pan gesture recognizer.sometimes touch in direction , label movement in other direction , more or less movement touch.
well totally depends on logics although wrote code you, hope you!! make sure uilabel userintraction set yes; , use touch began , touch moved methods instead of pangesture.... see now
-(void)touchesbegan:(nsset *)touches withevent:(uievent *)event { uitouch *touch = [[event alltouches]anyobject]; if([touch view] == lblname) { nslog(@"touch on label"); cgpoint pt = [[touches anyobject] locationinview:lblname]; startlocation = pt; // startlocation cgpoint declare globaly in .h..and lblname uilabel } } - (void) touchesmoved:(nsset *)touches withevent: (uievent *)event { uitouch *touch = [[event alltouches]anyobject]; if([touch view] == lblname) { cgpoint pt = [[touches anyobject] previouslocationinview:lblname]; cgfloat dx = pt.x - startlocation.x; cgfloat dy = pt.y - startlocation.y; cgpoint newcenter = cgpointmake(lblname.center.x + dx, lblname.center.y + dy); //now put if condition dragging in specific area cgfloat min_x = lblname.center.x + dx - lblname.frame.size.width/2.0; cgfloat max_x = lblname.center.x + dx + lblname.frame.size.width/2.0; cgfloat min_y = lblname.center.y + dy - lblname.frame.size.height/2.0; cgfloat max_y = lblname.center.y + dy + lblname.frame.size.height/2.0; if((min_x >= imageview.frame.origin.x && max_x <= imageview.frame.origin.x + imageview.frame.size.width) && (min_y >= imageview.frame.origin.y && max_y <= imageview.frame.origin.y + imageview.frame.size.height)) { lblname.center = newcenter; } } }
thank you!!
Comments
Post a Comment