IOS resize or reposition line on touching end points (Objective c) -
i trying resize line, touching red circle points. let's say, want move line above lips in image below, how can achieve this. line not moving position. have started thing , can't find relevant resources accomplish this. trying best...please guide me in right direction. below code , reference image.
objective c code:
- (void)viewdidload { [super viewdidload]; uibezierpath *path = [uibezierpath bezierpathwitharccenter:cgpointmake(100, 100) radius:10 startangle:0 endangle:6.2831853 clockwise:true]; //add second circle [path movetopoint:cgpointmake(100.0, 100.0)]; [path addlinetopoint:cgpointmake(200, 200)]; [path movetopoint:cgpointmake(200, 200)]; [path addarcwithcenter:cgpointmake(200, 200) radius:10 startangle:0 endangle:6.2831853 clockwise:true]; [path closepath]; [[uicolor redcolor] setstroke]; [[uicolor redcolor] setfill]; [path stroke]; [path fill]; cashapelayer *shapelayer = [cashapelayer layer]; shapelayer.path = [path cgpath]; shapelayer.strokecolor = [[uicolor bluecolor] cgcolor]; shapelayer.linewidth = 2.0; shapelayer.fillcolor = [[uicolor redcolor] cgcolor]; [self.view.layer addsublayer:shapelayer]; }
i add multiple lines , ultimate aim move individual line @ position in image , measurement of area using line size.
edit: scenario is, @ given time there flexible lines available on screen. lets's clicking button, 1 more new line added screen. user can drag end point resize line in direction. can't thing working correctly.. no luck.
here gist file link, https://gist.github.com/akhildave/3a8bec5b4df95cc06822 adds uiview create line on image. code in gist allows me resize line height moving touch points , down doesn't allow me rotate angle of line , adding text in centre. thanks
thanks!
one of previous answers can extended include features you're trying implement. need implement 2 classes: lineview
, lineimageview
.
before start, take @ animated gif of finished demo.
note "add line" button added in storyboard , not subview of image view. create button, style it, , hook ibaction
calls [self.lineimageview addlineview]
.
uiviewcontroller subclass implementation
#import "viewcontroller.h" #import "lineimageview.h" @interface viewcontroller () @property (weak, nonatomic) iboutlet lineimageview *lineimageview; @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; } - (ibaction)addlinetapped:(uibutton *)sender { [self.lineimageview addlineview]; } @end
lineview interface
#import <uikit/uikit.h> @interface lineview : uiview @property (nonatomic) cgpoint startpoint; @property (nonatomic) cgpoint endpoint; @property (nonatomic) cgfloat circleradius; // defaults 30.0 @end
lineview implementation
#import "lineview.h" @interface lineview () @property (nonatomic) bool startpointtracking; @property (nonatomic) bool endpointtracking; @end @implementation lineview - (instancetype)init { self = [super init]; if (self) { [self setup]; } return self; } - (instancetype)initwithcoder:(nscoder *)coder { self = [super initwithcoder:coder]; if (self) { [self setup]; } return self; } - (instancetype)initwithframe:(cgrect)frame { self = [super initwithframe:frame]; if (self) { [self setup]; self.startpoint = [self randompointinbounds]; self.endpoint = [self randompointinbounds]; [self setneedsdisplay]; } return self; } - (void)setup { self.backgroundcolor = [uicolor clearcolor]; self.multipletouchenabled = false; // multi-touch not allowed self.circleradius = 30.0; } #pragma mark - touch handling - (void)touchesbegan:(nsset *)touches withevent:(uievent *)event { uitouch *touch = [touches anyobject]; cgpoint location = [touch locationinview:self]; if ([self pointisonstartcircle:location]) { self.startpointtracking = yes; self.endpointtracking = no; } else if ([self pointisonendcircle:location]) { self.startpointtracking = no; self.endpointtracking = yes; } [self updatepointswithtouches:touches]; } - (void)touchesmoved:(nsset *)touches withevent:(uievent *)event { [self updatepointswithtouches:touches]; } - (void)touchesended:(nsset *)touches withevent:(uievent *)event { self.startpointtracking = no; self.endpointtracking = no; } - (void)touchescancelled:(nsset *)touches withevent:(uievent *)event { self.startpointtracking = no; self.endpointtracking = no; } - (void)updatepointswithtouches:(nsset *)touches { uitouch *touch = [touches anyobject]; if (self.startpointtracking) { self.startpoint = [touch locationinview:self]; [self setneedsdisplay]; } else if (self.endpointtracking) { self.endpoint = [touch locationinview:self]; [self setneedsdisplay]; } } - (bool)pointinside:(cgpoint)point withevent:(nullable uievent *)event { return [self pointisonstartcircle:point] || [self pointisonendcircle:point]; } #pragma mark - drawing - (void)drawrect:(cgrect)rect { if ([self ishidden]) { return; } [self drawtouchcircleatpoint:self.startpoint]; [self drawtouchcircleatpoint:self.endpoint]; [self drawlinebetweenfirstpoint:self.startpoint end:self.endpoint]; [self drawdistancetext]; } - (void)drawlinebetweenfirstpoint:(cgpoint)startpoint end:(cgpoint)endpoint { cgcontextref context = uigraphicsgetcurrentcontext(); cgcontextsavegstate(context); cgcontextsetstrokecolorwithcolor(context, [[[uicolor whitecolor] colorwithalphacomponent:0.6] cgcolor]); cgcontextsetlinewidth(context, 1.0); cgcontextmovetopoint(context, startpoint.x, startpoint.y); cgcontextaddlinetopoint(context, endpoint.x, endpoint.y); cgcontextstrokepath(context); cgcontextrestoregstate(context); } - (void)drawtouchcircleatpoint:(cgpoint)circlepoint { cgcontextref context = uigraphicsgetcurrentcontext(); cgcontextsavegstate(context); cgcontextsetlinewidth(context, 2.0); cgcontextsetrgbfillcolor(context, 1.0, 1.0, 1.0, 0.6); cgcontextaddarc(context, circlepoint.x, circlepoint.y, self.circleradius, 30.0, m_pi * 2, yes); cgcontextfillpath(context); cgcontextrestoregstate(context); } - (void)drawdistancetext { cgpoint midpoint = [self midpointbetweenfirstpoint:self.startpoint secondpoint:self.endpoint]; uifont *font = [uifont boldsystemfontofsize:18.0]; uicolor *textcolor = [uicolor whitecolor]; nsdictionary *attributes = @{nsfontattributename : font, nsforegroundcolorattributename : textcolor}; nsstring *distancestring = [self formatteddistancestring]; nsattributedstring *attributedstring = [[nsattributedstring alloc] initwithstring:distancestring attributes:attributes]; [attributedstring drawatpoint:midpoint]; } #pragma mark - helper methods - (cgpoint)randompointinbounds { int x = arc4random() % (int)cgrectgetwidth(self.bounds); int y = arc4random() % (int)cgrectgetheight(self.bounds); return cgpointmake(x, y); } - (cgfloat)distancefrompoint:(cgpoint)p1 topoint:(cgpoint)p2 { cgfloat xdist = p2.x - p1.x; cgfloat ydist = p2.y - p1.y; return sqrt((xdist * xdist) + (ydist * ydist)); } - (cgpoint)midpointbetweenfirstpoint:(cgpoint)p1 secondpoint:(cgpoint)p2 { cgfloat x = (p1.x + p2.x) / 2.0; cgfloat y = (p1.y + p2.y) / 2.0; return cgpointmake(x, y); } - (bool)pointisonstartcircle:(cgpoint)point { cgfloat distance = [self distancefrompoint:point topoint:self.startpoint]; return distance <= self.circleradius; } - (bool)pointisonendcircle:(cgpoint)point { cgfloat distance = [self distancefrompoint:point topoint:self.endpoint]; return distance <= self.circleradius; } - (nsstring *)formatteddistancestring { cgfloat distance = [self distancefrompoint:self.startpoint topoint:self.endpoint]; nsnumberformatter *formatter = [[self class] sharedformatter]; return [formatter stringfromnumber:@(distance)]; } + (nsnumberformatter *)sharedformatter { static nsnumberformatter *sharedformatter = nil; static dispatch_once_t oncetoken; dispatch_once(&oncetoken, ^{ sharedformatter = [[nsnumberformatter alloc] init]; }); return sharedformatter; }
lineimageview interface
#import <uikit/uikit.h> @interface lineimageview : uiimageview - (void)addlineview; @end
lineimageview implementation
#import "lineimageview.h" #import "lineview.h" @interface lineimageview () @property (nonatomic, strong) nsmutablearray *lineviews; // of lineview @end @implementation lineimageview - (instancetype)initwithframe:(cgrect)frame { self = [super initwithframe:frame]; if (self) { [self setup]; } return self; } - (instancetype)initwithcoder:(nscoder *)coder { self = [super initwithcoder:coder]; if (self) { [self setup]; } return self; } - (void)setup { self.userinteractionenabled = yes; } - (void)addlineview { lineview *lineview = [[lineview alloc] initwithframe:self.bounds]; [self addsubview:lineview]; [self.lineviews addobject:lineview]; }
Comments
Post a Comment