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

♀尐吖头ヾ 提交于 2020-01-13 05:35:09

问题


I want to make a project in which I have to touch one dot and connect it with another dot and after connect it to another dot. When I connect one dot with another dot the line will create between them.

Actually when I click/ touch one dot The line will show and When I touch second dot the line will create between the two dots.

I am not able to do this yet, I am trying and searching on the net but unable to find the solution yet.

This is my need Like this one https://play.google.com/store/apps/details?id=zok.android.dots&hl=en

I think this is done by UIGesture Recogniser? Or is this something else? How I can achieve this?

Any Idea or suggestions from experts would be highly welcome.


回答1:


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

}



回答2:


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.




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/16516343/how-to-connect-two-buttons-dots-with-a-line-in-ios

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