如何在iOS中连接两个button(点)?
我想做一个项目,我必须触摸一个点,并连接到另一个点,并连接到另一个点后。 当我连接一个点与另一个点时,线会在它们之间创build。
其实当我点击/触摸一个点线会显示,当我触摸第二个点时,线将创build两个点之间。
我无法做到这一点,我试图在网上search,但无法find解决scheme。
这是我的需要喜欢这一个https://play.google.com/store/apps/details?id=zok.android.dots&hl=en
我认为这是由UIGesture Recogniser完成的? 还是这是别的? 我怎么能做到这一点?
任何意见或build议的专家将非常欢迎。
根据您的要求修改此代码
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:
使CGPoint
touchStart
和touchEnd
实例,并得到他们这样的:
- (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]; }
您可以使用touchedEnded
方法将触摸的位置存储在两个不同的CGPoint
。
然后,当你有两个点时,你可以添加一个新的UIView作为子视图,它知道两个CGPoint
,并将在其drawRect
方法中绘制一条线。 或者在当前视图中调用[view setNeedsDisplay]
触发自己的drawRect方法。
看看这个链接 。
如果可能,请使用UI touch方法获取两个button的坐标。 在touchedEnded
方法的帮助下,您可以在两个不同的CGPoint
find触摸的位置。要查找触摸位置文档,请点击这里
在你的视图中获得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); }
希望这可以帮助
尝试下面的步骤。
创build一个UIView的子类。 添加你的UIButtons。
实施触摸分配,像touchesBegan,移动,结束。
里面touchesBegan检查是否触摸isinsideview:myButton1然后使一个标志为真。
编辑:
UITouch *touch = [[UITouch alloc] init]; touch = [touches anyObject]; CGPoint point = [touch locationInView:self]; if(CGRectContainsPoint(myButton1.frame, point)) NSLog(@"Inside myButton1");
另一种testing子视图是否被触摸命中的方法是
CGPoint pt = [[touches anyObject] locationInView:self.view]; UIView *touchedView = [self.view hitTest:pt withEvent:event];
里面触动移动检查是否真正的标志然后drawline()…并保持检查是否触摸是在insideview:myButton2。 调用setNeedsDisplay。
现在你将得到一些方法和示例代码,以在UIView中画线。 只要应用上面的逻辑。