从端点调整行大小

时间:2013-08-14 10:47:12

标签: ios annotations drawing drawrect resizable

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

我有一个Shape类的父类(从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];
        }
    }
}

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

为了移动和调整大小,我正在处理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的类变量; pointStart,pointEnd)和框架。

请建议调整大小的技巧来更新点和框架。

1 个答案:

答案 0 :(得分:0)

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

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

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

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

绘制线后,您只需要更新线的直线 为此,您需要在Line的类中再添加2个CGPoint属性 1.初始点,2。最后一点。
最初将初始点设置为等于起始点,并初始设置Last Point等效于End Point 然后在添加到当前Line的每一行上,只需通过与起点和终点的正确比较来更新这两个属性。我给你一些正确的比较:
例如:
比较Initial PointLast PointStart PointEnd Point的所有4个点。

更新初始点和最后一点:
Initial Point's'X'和'Y'设置为等于所有这4个点中的最小'X'和最小'Y'。
设置Last Point's'X'和'Y'相当于这4个点中的最大'X'和最大'Y' 根据这两个点Initial PointLast Point制作Line的Rect 我希望这能解决你的问题。