首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我如何在两条线之间画出一定角度的两条线,然后再拖动任何一条线就可以改变角度。

我如何在两条线之间画出一定角度的两条线,然后再拖动任何一条线就可以改变角度。
EN

Stack Overflow用户
提问于 2013-12-02 13:40:46
回答 1查看 663关注 0票数 0

我想让UBersense像应用程序(http://blog.ubersense.com/2013/01/03/how-to-use-the-drawing-tools-in-ubersense/),在那里我需要画两条线与一定的角度,然后我可以调整两条线之间的角度拖动任何线或交点。你们能给我一些想法或者代码片段吗。

截图网址:https://picasaweb.google.com/yunusm7/AppScreenshots#slideshow/5952787957718627714

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-02 15:33:52

你有一个有三个点的构造,一个点是一个角点,另外两个是顶点。首先,您应该创建一个如下所示的新类:

代码语言:javascript
运行
复制
@interface MyAngle : NSObject {

}

@property (nonatomic) CGPoint p1;
@property (nonatomic) CGPoint p2;
@property (nonatomic) CGPoint v; // this is an angle point

@end 

您可以使用该示例init的默认实现,而无需任何技巧:

代码语言:javascript
运行
复制
- (id)init {
   if (self = [super init]) {
       p1 = CGPointMake(1,0);
       p2 = CGPointMake(0,1);
       v = CGPointZero;
   }
   return self;
}

但正如我所理解的,你需要知道这个角度的价值。您可以使用以下方式完成此操作:

代码语言:javascript
运行
复制
- (CGFloat)valueOfAngle {
    CGPoint v1 = CGPointMake(p1.x-v.x, p1.y-v.y);
    CGPoint v2 = CGPointMake(p2.x-v.x, p2.y-v.y);

    CGFloat scalarProduct = v1.x*v2.x + v1.y*v2.y;
    CGFloat lengthProduct = sqrt(v1.x*v1.x + v1.y*v1.y)*sqrt(v2.x*v2.x + v2.y*v2.y);
    CGFloat fraction = scalarProduct / lengthProduct;
    if (fraction < -1) fraction = -1;
    if (fraction > 1) fraction = 1;

    return acos(fraction);
}

如果你想获得超过180度的角度,你应该改变上面的代码一点。但是有太多的信息关于如何在互联网上这样做,所以我将跳过这一部分。

然后您需要在您的MyAngle中创建一个viewController实例。让它被称为“角度”。知道每三个点的坐标,如果足够的话就画它(!)。在包含drawRect实例的视图中实现MyAngle方法(我强烈建议在您自己的UIView子类上这样做):

代码语言:javascript
运行
复制
- (void)drawRect {
    [super drawRect];

    // set options of drawing
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
    CGContextSetLineWidth(c, 3.0);
    CGContextSetStrokeColor(c, red);

    // draw an angle directly
    CGContextBeginPath(c);
    CGContextMoveToPoint(c, angle.p1.x, angle.p1.y);
    CGContextAddLineToPoint(c, angle.v.x, angle.v.y);
    CGContextAddLineToPoint(c, angle.p2.x, angle.p2.y);
    CGContextStrokePath(c);

    // draw circles around vertices (like on the screenshot you provided)
    CGFloat R = 7.0f;
    CGContextBeginPath(c);
    CGContextAddEllipseInRect(c, CGRectMake(angle.p1.x - R, angle.p1.y - R, 2*R, 2*R));
    CGContextStrokePath(c);

    CGContextBeginPath(c);
    CGContextAddEllipseInRect(c, CGRectMake(angle.p2.x - R, angle.p2.y - R, 2*R, 2*R));
    CGContextStrokePath(c);

    CGContextBeginPath(c);
    CGContextAddEllipseInRect(c, CGRectMake(angle.v.x - R, angle.v.y - R, 2*R, 2*R));
    CGContextStrokePath(c);
}

这就是你画你想要的东西所需要知道的!如果需要,可以更改三个圆圈的笔画颜色或半径。

然后你需要有可能改变你的角度点的位置。为此,您只需在viewController的viewDidLoad方法中实现如下所示的viewDidLoad:

代码语言:javascript
运行
复制
UIPanGestureRecognizer *pan = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveAngle:)] autorelease];
pan.delegate = self;
[self.view addGestureRecognizer:pan]; 

实现UIGestureRecognizerDelegate方法:

代码语言:javascript
运行
复制
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
        CGPoint p = [gestureRecognizer locationInView:self.view];
        CGFloat d1 = sqrt((p.x-angle.p1.x)*(p.x-angle.p1.x) + (p.y-angle.p1.y)*(p.y-angle.p1.y);
        CGFloat d2 = sqrt((p.x-angle.p2.x)*(p.x-angle.p2.x) + (p.y-angle.p2.y)*(p.y-angle.p2.y);
        CGFloat d3 = sqrt((p.x-angle.v.x)*(p.x-angle.v.x) + (p.y-angle.v.y)*(p.y-angle.v.y);

        // just check if we touched the screen near some of angle's points
        CGFloat tolerance = 15.0f;
        return (d1 < tolerance) || (d2 < tolerance) || (d3 < tolerance);
    }
    return YES;
}

和塔格勒特的选择器(也在您的viewController中):

代码语言:javascript
运行
复制
- (void)moveAngle:(UIPanGestureRecognizer *)gr {
    CGPoint p = [gr locationInView:self.view];

    if (gr.state == UIGestureRecognizerStateBegan) {
        CGFloat d1 = sqrt((p.x-angle.p1.x)*(p.x-angle.p1.x) + (p.y-angle.p1.y)*(p.y-angle.p1.y);
        CGFloat d2 = sqrt((p.x-angle.p2.x)*(p.x-angle.p2.x) + (p.y-angle.p2.y)*(p.y-angle.p2.y);
        CGFloat d3 = sqrt((p.x-angle.v.x)*(p.x-angle.v.x) + (p.y-angle.v.y)*(p.y-angle.v.y);

        // pointToMove is your int variable
        CGFloat tolerance = 15.0f;
        if (d1 < tolerance) {
            pointToMove = 1;
        }
        else if (d2 < tolerance) {
            pointToMove = 2;
        }
        else {
            pointToMove = 3;
        }
    }
    else {
        if (pointToMove == 1) {
            angle.p1 = loc;
        }
        else if (pointToMove == 2) {
            angle.p2 = loc;
        }
        else {
            angle.v = loc;
        }
        [yourCustomView setNeedsDisplay];
        [yourLabel setText:[NSString stringWithFormat:@"%.3f", [angle valueOfangle]*180/PI]];
    }
}

也许我跳过了一些显而易见的东西,但我认为开始编写一些代码就足够了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20329742

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档