How to connect two buttons( dots) with a line in iOS? [duplicate]

余生长醉 提交于 2019-12-04 16:03:39
Divyu

Modify this code according to your requiements

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

@Nisha:

Make gloabal instances of CGPoint touchStart and touchEnd and get them like this:

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

}
iDeveloper

You can store the touched locations in two different CGPoint with the help of the touchedEnded method.

Then, when you have your two points, you can add a new UIView as subview which is aware of the two CGPoint and will draw a line in its drawRect method. Or do it in the current view, by calling [view setNeedsDisplay] to trigger its own drawRect method.

Check out this link.

If it is possible for you please get the coordinates of two button with UI touch methods. You can find the touched locations in two different CGPoint with the help of the touchedEnded method.To find touch location documentation is here

After getting the location of UIButtons on your view you can draw the line between then with this method-

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

Hope this helps

BhushanVU

Try below steps.

Create one subclass of UIView. Add your UIButtons on it.

Implement Touches delgates, like touchesBegan, moved, end.

Inside touchesBegan check if touch isinsideview:myButton1 then make a flag true.

EDIT:

UITouch *touch = [[UITouch alloc] init];
touch = [touches anyObject];
CGPoint point = [touch locationInView:self];

if(CGRectContainsPoint(myButton1.frame, point))
    NSLog(@"Inside myButton1");

Another way to test if subview is hit by touches is

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

inside touches moved check if flag true then drawline()... and keep checking if touches are in insideview:myButton2. call setNeedsDisplay.

Now you will get number of ways and sample code to draw line in UIView. Just apply above logic.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!