从端点调整行大小

我在一个视图上绘制注释。 线条注释引起问题;

我有一个父类的形状(从UIView扩展)。 所有的注释都是形状的子类。 在每个注解类中,我都覆盖了drawRect方法。 DrawingCanvas类(从UIView扩展,是我的viewController的父视图的子视图)处理平移手势。 这是一段代码

-(void) panGestureRecognizer: (UIPanGestureRecognizer *) panGesture { static CGPoint initialPoint; CGPoint translation = [panGesture translationInView:panGesture.view]; static Shape *shape; if(panGesture.state == UIGestureRecognizerStateBegan) { initialPoint = [panGesture locationInView:panGesture.view]; if(selectedShape == nil) { if([selectedOption isEqualToString:@"line"]) { shape = [[Line alloc] initWithFrame:CGRectMake(initialPoint.x, initialPoint.y, 10, 10) isArrowLine:NO]; ((Line *)shape).pointStart = [panGesture.view convertPoint:initialPoint toView:shape]; } [panGesture.view addSubview:shape]; } [shape setNeedsDisplay]; } else if(panGesture.state == UIGestureRecognizerStateChanged) { if([shape isKindOfClass:[Line class]]) { CGRect newRect = shape.frame; if (translation.x < 0) { newRect.origin.x = initialPoint.x + translation.x - LINE_RECT_OFFSET; newRect.size.width = fabsf(translation.x) + LINE_RECT_OFFSET * 2; } else { newRect.size.width = translation.x + LINE_RECT_OFFSET * 2; } if (translation.y < 0) { newRect.origin.y = initialPoint.y + translation.y - LINE_RECT_OFFSET; newRect.size.height = fabsf(translation.y) + LINE_RECT_OFFSET * 2; } else { newRect.size.height = translation.y + LINE_RECT_OFFSET * 2; } shape.frame = newRect; CGPoint endPoint = CGPointMake(initialPoint.x + translation.x, initialPoint.y + translation.y); ((Line *)shape).pointStart = [panGesture.view convertPoint:initialPoint toView:shape]; ((Line *)shape).pointEnd = [panGesture.view convertPoint:endPoint toView:shape]; [shape setNeedsDisplay]; } } } 

线drawRect包含

 -(void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextClearRect(context, rect); CGContextSetStrokeColorWithColor(context, self.color.CGColor); CGContextSetFillColorWithColor(context, self.color.CGColor); CGContextMoveToPoint(context, pointStart.x, pointStart.y); CGContextAddLineToPoint(context, pointEnd.x, pointEnd.y); CGContextSetLineWidth(context, 2.f); CGContextStrokePath(context); } 

为了移动和resize,我正在处理touchEvents为了移动注释,我在touchesMoved中做了这个

 UITouch *touch = [[event allTouches] anyObject]; CGPoint newPoint = [touch locationInView:self]; CGPoint previousPoint = [touch previousLocationInView:self]; CGRect rect = self.frame; rect.origin.x += newPoint.x - previousPoint.x; rect.origin.y += newPoint.y - previousPoint.y; self.frame = rect; [self setNeedsDisplay]; 

在这里一切都很好,但是现在通过拖动端点来调整行的大小会给我造成困惑。 我已经把imageViews放在了终点。 触摸开始,我正在检测像这样的终点

 UITouch *touch = [[event allTouches] anyObject]; CGPoint touchPoint = [touch locationInView:self]; if(CGRectContainsPoint(imageView1.frame, touchPoint) || CGRectContainsPoint(imageView2.frame, touchPoint)) { isEndPoint = YES; } 

如果isEndPoint是YES,那么我必须调整行大小。 我需要知道我拖着哪个方向。 如何更新点(我已经采取了CGPoint的类variables; pointStart,pointEnd)和框架。

请build议resize的技巧来更新点和框架。

为了确定线路的方向:
您可以执行以下操作:

从起点开始行
a) Greater 'X' than the Start Point's X线更接近右侧
b) Less 'X' than the Start Point's X线更接近左侧的Less 'X' than the Start Point's X
c) Greater Y'' than the Start point's 'Y'线Greater Y'' than the Start point's 'Y'正在向下。
d) Less 'Y' than the Start point's 'Y'线Less 'Y' than the Start point's 'Y'线正在向上。

从终点开始行:
a) Greater 'X' than the End Point's X线更接近右侧的Greater 'X' than the End Point's X
b) Less 'X' than the End Point's X线靠近左侧的Less 'X' than the End Point's X
c) Greater Y'' than the End point's 'Y'线Greater Y'' than the End point's 'Y'正在向下。
d) Less 'Y' than the End point's 'Y'线Less 'Y' than the End point's 'Y'线向上。

如果从终点绘制线,则保持起点不变,否则如果线从起点开始,则保持起点不变。

绘制线后,您只需要更新线的矩形。
为此,您需要再添加2个CGPoint属性到Line的类。
1.初始点,2.最后一点。
初始设置初始点相当于开始点,最初设置最后一个点相当于结束点。
之后,在添加到当前Line每一Line ,只需更新这两个属性,并适当比较开始点和结束点。 我给你一点适当的比较:
例如:
比较所有4个点,即Initial PointEnd PointStart PointEnd Point

更新初始点和最后一点:
在所有这4点中,将Initial Point's “X”和“Y”设置为最小值“X”和最小值“Y”。
设置Last Point's “X”和“Y”等同于这4个点中的最大值“X”和最大值“Y”。
根据这2点Initial PointLast Point生成线路矩形。
我希望这能解决你的问题。