从端点调整线的大小 [英] Resize line from endpoints

查看:20
本文介绍了从端点调整线的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在视图上绘制注释.行注释导致问题;

I am drawing annotations on a view. The line annotation is causing a problem;

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

I have a parent class of Shape (extended from UIView). All the annotations are subclass of shape. In each annotation class i have override the drawRect method. DrawingCanvas class (extended from UIView and is a subview of my viewController's parent view) handles the pan gesture. Here is a piece of code

-(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];
        }
    }
}

Line drawRect 包含

Line drawRect contains

-(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);
}

为了移动和调整大小,我正在处理 touchEvents为了移动注释,我在 touchesMoved

For moving and resizing i am handling touchEvents For moving the annotation i am doing this in 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.在触摸开始时,我正在检测这样的终点

It's all good till here, but now for resizing the line by dragging the endpoints of it creates the confusion to me. I have placed to imageViews at the end point. On touches began i am detecting the end point like this

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

如果 isEndPoint 是 YES 那么我必须调整线的大小.我需要知道我在哪个方向拖动.如何更新点(我已经采用了 CGPoint 的类变量;pointStart、pointEnd)和框架.

if isEndPoint is YES then i have to resize the line. I need to know on which direction am i dragging. how to update the points (i have taken to class variables of CGPoint; pointStart, pointEnd) and the frame.

请建议调整大小技巧以更新点和框架.

Kindly suggest the resizing trick to update the points and the frame.

推荐答案

用于确定线的方向:
您可以执行以下操作:

For determining the line's direction:
You can do the following:

从起点开始的线:
a) 比起点的 X 大的 'X' 线正在靠近右侧
b) 比起点的 X 少的 'X' 线正在接近左侧.
c) Greater Y'' than the Start point's 'Y' 线向下.
d) 比起点的 'Y' 少的 'Y' 线向上.

Line starting from Start Point:
a) Greater 'X' than the Start Point's X Line is approaching towards right side
b) Less 'X' than the Start Point's X line is approaching to the left side.
c) Greater Y'' than the Start point's 'Y' Line is going downwards.
d) Less 'Y' than the Start point's 'Y' Line is going Upwards.

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

Line starting from End Point:
a) Greater 'X' than the End Point's X Line is approaching towards right side
b) Less 'X' than the End Point's X line is approaching to the left side.
c) Greater Y'' than the End point's 'Y' Line is going downwards.
d) Less 'Y' than the End point's 'Y' Line is going Upwards.

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

If the line is being drawn from the End point then keep the Start Point unchanged, else if the line is starting from Start Point, keep the Start Point unchanged.

画完直线后,只需要更新直线的Rect即可.
为此,您需要向 Line 的 Class 添加另外 2 个 CGPoint 属性.
1. 起点,2. 终点.
初始设置初始点等于起点,初始设置最后点等于终点.
之后,在添加到当前 Line 的每一行上,只需更新这 2 个属性,并与起点和终点进行适当的比较.我给你一些适当的比较提示:
例如:
比较Initial PointLast PointStart PointEnd Point这4个点.

更新起点和终点:
设置 Initial Point's 'X' 和 'Y' 相当于所有这 4 个点中的最小 'X' 和最小 'Y'.
设置 Last Point's 'X' 和 'Y' 相当于这 4 个点中最大的 'X' 和最大的 'Y'.
根据这2个点Initial PointLast Point制作直线的矩形.
我希望这能解决您的问题.

After drawing the line, you just need to update the Rect of the Line.
For that you need to add 2 more CGPoint properties to the Line's Class.
1. Initial point, 2. Last Point.
Initially set the initial point equivalent to the Start Point and initially set the Last Point equivalent to the End Point.
Afterwards on every piece of line added to the Current Line, just update these 2 properties with proper comparison to the Start and End Points. I am giving you a hint of proper comparisons:
For example:
Compare all the 4 points that is Initial Point, Last Point , Start Point and End Point.

Updating Initial Point and Last Point:
Set the Initial Point's 'X' and 'Y' equivalent to the Minimum 'X' and Minimum 'Y' among all these 4 points.
Set the Last Point's 'X' and 'Y' equivalent to the Maximum 'X' and Maximum 'Y' among these 4 points.
Make the Line's Rect according to these 2 points Initial Point and Last Point.
I hope this solves your problem.

这篇关于从端点调整线的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆