ios - UIGestureRecognizerStateChangedv called twice when moving bezier image in a subView -
i implemented square draw using uibezierpath. in order move same square, implemented uilongpressgesturerecognizer can check if square pressed , move square freely in subview.
here of code:
square.h ... //square coordinates , size static const float xpoint = 10; static const float ypoint = 16; static const float width = 10; static const float height = 20; //bezier bitmap context static const float contextwidht = 300; static const float contextheigh = 300; ... square.m ... -(void)build{ uibezierpath *squaredraw = [uibezierpath bezierpathwithrect:cgrectmake(xpoint, ypoint, width, height)]; uigraphicsbeginimagecontext(cgsizemake(contextwidht, contextheigh)); // graphic context cgcontextref context = uigraphicsgetcurrentcontext(); cgcontextsetstrokecolorwithcolor(context, [uicolor bluecolor].cgcolor); cgcontextsetfillcolorwithcolor(context, [uicolor clearcolor].cgcolor); [squaredraw fill]; [squaredraw stroke]; // image graphic context uiimage *bezierimage = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); uiimageview *bezierimageview = [[uiimageview alloc]initwithimage:bezierimage]; //subview _frame = bezierimageview; [_frame setuserinteractionenabled:yes]; //gesture recognizer uilongpressgesturerecognizer *tapgesture = [[uilongpressgesturerecognizer alloc] initwithtarget:self action:@selector(taphandler:)]; tapgesture.delegate = (id)self; [_frame addgesturerecognizer:tapgesture]; [self addsubview:_frame]; } - (void)taphandler:(uilongpressgesturerecognizer *) sender{ sender.delegate = (id)_frame; cgpoint touchlocation = [sender locationinview:_frame]; cgfloat xvariation, yvariation; ... case uigesturerecognizerstatechanged: xvariation = [sender locationinview:_frame].x; yvariation = [sender locationinview:_frame].y; _frame.center = cgpointmake(xvariation, yvariation); break; .... }
seems like, when debugging app ( press , move _frame ), "uigesturerecognizerstatechanged" called twice , returning alternated points between subview , points result contextwidth , contextheight:
2013-07-22 14:55:00.327 bezier[10847:11603] xvariation: 22.000000 2013-07-22 14:55:00.329 bezier[10847:11603] yvariation: 28.000000 2013-07-22 14:55:00.330 bezier[10847:11603] xvariation: 150.000000 ----> ?? 2013-07-22 14:55:00.330 bezier[10847:11603] yvariation: 150.000000 ----> ?? 2013-07-22 14:55:00.332 bezier[10847:11603] xvariation: 29.000000 2013-07-22 14:55:00.332 bezier[10847:11603] yvariation: 28.000000
why so?
uilongpressgesturerecognizer sends data continuously. if want 1 signal need handle in delegate , watch state. documentation:
long-press gestures continuous. gesture begins (uigesturerecognizerstatebegan) when number of allowable fingers (numberoftouchesrequired) have been pressed specified period (minimumpressduration) , touches not move beyond allowable range of movement (allowablemovement). gesture recognizer transitions change state whenever finger moves, , ends (uigesturerecognizerstateended) when of fingers lifted.
Comments
Post a Comment