ios - Objective C - Sketch Application will not draw long lines -
i have iphone app provide sketch pad user save signature. uiimageview gets added main view , holds strokes. reason can draw short lines on pad following image.
i have application ipad uses same code , works fine. i'm not sure causing it. i'm not using touch or gesture code interfere it. following of code use.
update: if create uiviewcontroller same class , make root view controller works fine. in navigation hierarchy doing weird.
-(void)setupsignaturepad{ //create frame our signature capture imageframe = cgrectmake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width + 23, self.view.frame.size.height + 7 ); //allocate image view , add main view mysignatureimage = [[uiimageview alloc] initwithimage:nil]; mysignatureimage.frame = imageframe; mysignatureimage.backgroundcolor = [uicolor whitecolor]; [self.view addsubview:mysignatureimage]; } //when 1 or more fingers touch down in view or window - (void)touchesbegan:(nsset *)touches withevent:(uievent *)event { //did our finger moved yet? fingermoved = no; uitouch *touch = [touches anyobject]; //we need 3 points of contact make our signature smooth using quadratic bezier curve currentpoint = [touch locationinview:mysignatureimage]; lastcontactpoint1 = [touch previouslocationinview:mysignatureimage]; lastcontactpoint2 = [touch previouslocationinview:mysignatureimage]; //when 1 or more fingers associated event move within view or window - (void)touchesmoved:(nsset *)touches withevent:(uievent *)event { //well obvious our finger moved on screen fingermoved = yes; uitouch *touch = [touches anyobject]; //save previous contact locations lastcontactpoint2 = lastcontactpoint1; lastcontactpoint1 = [touch previouslocationinview:mysignatureimage]; //save current location currentpoint = [touch locationinview:mysignatureimage]; //find mid points used quadratic bezier curve cgpoint midpoint1 = [self midpoint:lastcontactpoint1 withpoint:lastcontactpoint2]; cgpoint midpoint2 = [self midpoint:currentpoint withpoint:lastcontactpoint1]; //create bitmap-based graphics context , makes current context uigraphicsbeginimagecontext(imageframe.size); //draw entire image in specified rectangle frame [mysignatureimage.image drawinrect:cgrectmake(0, 0, imageframe.size.width, imageframe.size.height)]; //set line cap, width, stroke color , begin path cgcontextsetlinecap(uigraphicsgetcurrentcontext(), kcglinecapround); cgcontextsetlinewidth(uigraphicsgetcurrentcontext(), 3.0f); cgcontextsetrgbstrokecolor(uigraphicsgetcurrentcontext(), 0.0, 0.0, 0.0, 1.0); cgcontextbeginpath(uigraphicsgetcurrentcontext()); //begin new new subpath @ point cgcontextmovetopoint(uigraphicsgetcurrentcontext(), midpoint1.x, midpoint1.y); //create quadratic bézier curve current point using control point , end point cgcontextaddquadcurvetopoint(uigraphicsgetcurrentcontext(), lastcontactpoint1.x, lastcontactpoint1.y, midpoint2.x, midpoint2.y); //set miter limit joins of connected lines in graphics context cgcontextsetmiterlimit(uigraphicsgetcurrentcontext(), 2.0); //paint line along current path cgcontextstrokepath(uigraphicsgetcurrentcontext()); //set image based on contents of current bitmap-based graphics context mysignatureimage.image = uigraphicsgetimagefromcurrentimagecontext(); //remove current bitmap-based graphics context top of stack uigraphicsendimagecontext(); } - (void)touchesended:(nsset *)touches withevent:(uievent *)event { //if finger never moved draw point if(!fingermoved) { uigraphicsbeginimagecontext(imageframe.size); [mysignatureimage.image drawinrect:cgrectmake(0, 0, imageframe.size.width, imageframe.size.height)]; cgcontextsetlinecap(uigraphicsgetcurrentcontext(), kcglinecapround); cgcontextsetlinewidth(uigraphicsgetcurrentcontext(), 3.0f); cgcontextsetrgbstrokecolor(uigraphicsgetcurrentcontext(), 0.0, 0.0, 0.0, 1.0); cgcontextmovetopoint(uigraphicsgetcurrentcontext(), currentpoint.x, currentpoint.y); cgcontextaddlinetopoint(uigraphicsgetcurrentcontext(), currentpoint.x, currentpoint.y); cgcontextstrokepath(uigraphicsgetcurrentcontext()); cgcontextflush(uigraphicsgetcurrentcontext()); mysignatureimage.image = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); } } //calculate midpoint between 2 points - (cgpoint) midpoint:(cgpoint )p0 withpoint: (cgpoint) p1 { return (cgpoint) { (p0.x + p1.x) / 2.0, (p0.y + p1.y) / 2.0 }; }
i'm sorry tell haven't real solution, problem due performance issues. why? because creating image each time gesture detected. creating images requires of screen renderings takes time , resources.
should base code on same project has drawing functionalities, use view updates draw in drawrect method, maybe cashapelaywr fine.
run time profiler in instruments , search wich method tanking time.
Comments
Post a Comment