背景
我有一个应用程序,用户可以在其中绘制图表。用户必须通过在图表上滑动来绘制此图表。图表应根据用户滑动绘制垂直和水平线。我有这个工作
问题
我有一个UIImageView
,绘图完成。我的问题是我必须限制用户绘制的任何一行,以便不相互重叠。简单的话我不想要这些行重叠或相互交叉。我已经使用下面的视图触摸事件实现了绘图。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
fromPoint = [touch locationInView:_drawingPad];
/*This is for my segment detection .i need to start from the
middle of any given segment irrespective of the user point
of touch in that segment.*/
if (fromPoint.y < firstSegment.y) {
fromPoint.y = firstSegment.y - 30.5;
}else if (fromPoint.y < seconSegment.y){
fromPoint.y = seconSegment.y - 30.5;
} else if (fromPoint.y < thirdSegment.y){
fromPoint.y = thirdSegment.y - 30.5;
}else{
fromPoint.y = fourthSegment.y - 30.5;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
toPoint = [touch locationInView:_drawingPad];
CGPoint currentPoint = [touch locationInView:_drawingPad];
/*This to restrict lines to only being vertical
and horizontal Code courtesy stack overflow*/
double slope = fabs((toPoint.y - fromPoint.y) / (toPoint.x - fromPoint.x));
if (fromPoint.y == toPoint.y)
{
lineRot = horizontal;//lineRot is an enum
}
if (slope > 1){
lineRot = vertical;
}
else if(slope < 1){
lineRot = horizontal;
}
if (toPoint.y < firstSegment.y) {
toPoint.y = firstSegment.y - 30.5;
}else if (toPoint.y < seconSegment.y){
toPoint.y = seconSegment.y - 30.5;
} else if (toPoint.y < thirdSegment.y){
toPoint.y = thirdSegment.y - 30.5;
}else{
toPoint.y = fourthSegment.y - 30.5;
}
if (toPoint.x < x) {//This is to avoid a back trace,x is initialized to zero in viewDidLoad
return;
} else {
x = toPoint.x;
if (!CGPointEqualToPoint(fromPoint, CGPointZero)) {
UIGraphicsBeginImageContext(_drawingPad.frame.size);
[_drawingPad.image drawInRect:CGRectMake(0, 0, _drawingPad.frame.size.width, _drawingPad.frame.size.height)];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextBeginPath(context);
CGContextMoveToPoint(context,fromPoint.x, fromPoint.y);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 1.0, 1.0);
if (lineRot == horizontal) {
CGContextAddLineToPoint(context, toPoint.x, fromPoint.y);
} else{
CGContextAddLineToPoint(context, fromPoint.x, toPoint.y);
}
CGContextClosePath(context);
CGContextStrokePath(context);
_drawingPad.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
}
我尝试过什么
不幸的是没有。事实是我不知道从哪里开始。我看到了这个Hit detection when drawing lines in iOS但是无法得到正在发生的事情以及如何实现。非常感谢任何帮助