【问题标题】:How to connect two buttons( dots) with a line in iOS? [duplicate]如何在 iOS 中用一条线连接两个按钮(点)? [复制]
【发布时间】:2013-05-13 06:54:28
【问题描述】:

我想做一个项目,我必须触摸一个点并将其与另一个点连接,然后将其连接到另一个点。当我将一个点与另一个点连接时,线将在它们之间创建。

实际上,当我单击/触摸一个点时会显示线条,当我触摸第二个点时,会在两个点之间创建线条。

我还不能这样做,我正在网上尝试和搜索,但还没有找到解决方案。

这是我的需要 喜欢这个https://play.google.com/store/apps/details?id=zok.android.dots&hl=en

我认为这是由 UIGesture Recogniser 完成的?或者这是别的什么?我怎样才能做到这一点?

非常欢迎专家提出任何想法或建议。

【问题讨论】:

标签: iphone ios uibutton uigesturerecognizer uitouch


【解决方案1】:

根据您的要求修改此代码

CGContextRef context = UIGraphicsGetCurrentContext();
UIColor *currentColor = [UIColor blackColor];
CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
CGContextSetLineWidth(context, 2.0);
CGContextBeginPath(context);
CGContextMoveToPoint(context, touchStart.x, touchStart.y);
CGContextAddLineToPoint(context, touchEnd.x, touchEnd.y);
CGContextStrokePath(context);

@妮莎:

制作CGPoint touchStarttouchEnd 的全局实例,然后像这样得到它们:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
touchEnd = CGPointZero;
touchStart = CGPointZero;
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
NSLog(@"start point >> %@",NSStringFromCGPoint(point));
    touchStart = point;
}

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    touchEnd = point; 
    [self setNeedsDisplay];

}

【讨论】:

  • touchStart 是什么,touchEnd 是 int 吗?
  • @Nisha Singh ...我已经编辑了我的答案检查它..
【解决方案2】:

您可以借助touchedEnded 方法将触摸的位置存储在两个不同的CGPoint 中。

然后,当你有两个点时,你可以添加一个新的 UIView 作为子视图,它知道两个 CGPoint 并将在其 drawRect 方法中画一条线。或者在当前视图中,调用[view setNeedsDisplay]触发自己的drawRect方法。

看看这个link

【讨论】:

    【解决方案3】:

    如果可能,请使用 UI 触摸方法获取两个按钮的坐标。您可以借助touchedEnded 方法在两个不同的CGPoint 中找到触摸位置。查找触摸位置文档是here

    在您的视图上获得UIButtons 的位置后,您可以使用此方法在它们之间画线-

    - (void)drawRect:(CGRect)rect
    {
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSaveGState(context);
        CGContextSetStrokeColorWithColor(context, [[UIColor blackColor]CGColor]);
        CGContextSetLineWidth(context, 1.0);
        CGContextMoveToPoint(context, startPoint.x, startPoint.y);
        CGContextAddLineToPoint(context, endPoint.x, endPoint.y);
        CGContextStrokePath(context);
        CGContextRestoreGState(context); 
    }
    

    希望对你有帮助

    【讨论】:

    • touchStart 是什么,touchEnd 是这个 int 吗?
    • 这是按钮的坐标,它们是具有两个值 x 和 y 的结构,它们是浮点值。希望现在清楚
    • 先生不清楚,我是 iPhone 新手。大佬这里的UIButton怎么用?
    • 在哪里?我不明白。你想画线的按钮在屏幕上总是有一些坐标。使用这些坐标在它们之间画线
    • 你的问题解决了吗?
    【解决方案4】:

    尝试以下步骤。

    创建一个 UIView 的子类。在上面添加你的 UIButtons。

    实现 Touches 委托,例如 touchesBegan、moved、end。

    Inside touchesBegan 检查 touch isinsideview:myButton1 是否为真。

    编辑:

    UITouch *touch = [[UITouch alloc] init];
    touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    
    if(CGRectContainsPoint(myButton1.frame, point))
        NSLog(@"Inside myButton1");
    

    另一种测试子视图是否被触摸击中的方法

    CGPoint pt = [[touches anyObject] locationInView:self.view];
    UIView *touchedView = [self.view hitTest:pt withEvent:event];
    

    inside touches 移动检查标志是否为 true 然后 drawline()... 并继续检查 touches 是否在 insideview:myButton2 中。调用 setNeedsDisplay。

    现在您将获得在 UIView 中绘制线条的多种方法和示例代码。只需应用上述逻辑即可。

    【讨论】:

    • 先生,我现在可以画线了,但我不能按照您说的做。我在 uiview 上添加了按钮,但无法执行您的 3,4 步骤。
    • 检查已编辑的答案..
    • 我将按钮放在 viewDidLoad 上,添加到 self.view 上,然后我执行您的第三步。当我触摸屏幕时显示线条,但是当我触摸按钮时,线条与按钮相交时没有触摸。此行没有调用 if(CGRectContainsPoint(btn1.frame, point));
    • 您的按钮应该是您正在绘制的视图的子视图。是这样吗?
    • hitTest: 这个方法可以达到你的目的...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-13
    • 1970-01-01
    相关资源
    最近更新 更多