核心graphics线条画和删除触摸

我正在写一个iPhone应用程序,有点像拿起appstore上的应用程序。 我已经完成了绘制多行并使用CoreGraphics显示它。 但是现在我遇到了如何在用户点击时删除这一行。 我在Google上search了很多,但没有find任何相关的信息。


我在其他地方见过你的这篇文章,这真的帮了我一个忙。 但问题是,如果我有一根棍子,然后在另一根棍子上。 当我点击下面的棍子,它不应该被删除,如果我点击上面的,它应该被删除,如果没有棍子上面。 如何实现这个事情..另外我需要存储的path,即我的行是UIBeizerpath在列表,数组,字典什么的,请帮助我这个东西也。 我没有find任何东西。

我提到了两个问题:(1)如何检测视图中画出的线条;(2)如何在没有触及的线条的情况下重绘视图。 组合的方法是将一个线对象定义为矩形转换为string: NSStringFromCGRect(CGRectMake(x, y, width, height) 。将一个或多个线对象添加到NSSet *setOfLines并添加Quartz 2D / Core Graphics绘图代码在显示行的UIView类的drawRect:方法中。

 - (void)drawRect:(CGRect)rect { gc = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(gc, lineThickness]); CGContextSetStrokeColorWithColor(gc, ([UIColor redColor]).CGColor); for (NSString *lineString in setOfLines) { CGRect line = CGRectFromString(lineString); CGContextMoveToPoint(gc, line.origin.x, line.origin.y); CGContextAddLineToPoint(gc, (line.origin.x + line.size.width), (line.origin.y + line.size.height)); } CGContextStrokePath(gc); } 

在同一个UIView类中,添加一个轻击手势识别器(注意与双击共存):

 UITapGestureRecognizer *singleTapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lineSingleTapped:)]; singleTapper.numberOfTapsRequired = 1; [singleTapper requireGestureRecognizerToFail:doubleTapper]; [cell addGestureRecognizer:singleTapper]; - (IBAction)lineSingleTapped:(UITapGestureRecognizer *)sender { CGPoint tapPoint = [sender locationInView:self]; // Get the line that was tapped for (NSString *lineString in setOfLines) { CGRect line = CGRectFromString(lineString); // Make a rectangle that defines the tappable area of a line: disclaimer, might be very thin or short! // A minimum size of 44.0 x 44.0 is advisable CGRect lineTapArea = CGRectMake(line.origin.x, line.origin.y, (line.origin.x + line.size.width), line.origin.y + line.size.height); if (CGRectContainsPoint(lineTapArea, tapPoint)) { [setOfLines removeObject:lineObject]; [self setNeedsDisplayInRect:lineTapArea]; break; } } } 

这里的devise模式是:

  1. 实现绘图视图类为用户交互和做绘画
  2. 定义表示要绘制的视觉元素的数据对象
  3. 根据用户交互改变数据对象,然后强制部分或全部视图重新绘制

如果您在UIView中使用Core Graphics,则不必删除任何内容。 相反,您可以完全重新绘制视图,但也许只需less一个棍子。