背景
我有一个应用程序,用户可以在其中绘制图表。用户必须通过在图表上滑动来绘制此图表。图表应根据用户的滑动绘制垂直和水平线。我已经完成了这项工作。
问题
我有一个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();
}
}
}
我试过的
不幸的是没有。事实是我不知道从哪里开始。我在 iOS 中绘制线条时看到了这个命中检测,但无法了解发生了什么以及如何实现。非常感谢任何帮助