3
我想开发一个应用程序来绘制触摸iPhone屏幕搜索如何做到这一点我已经找到了一个示例配方(09-油漆)到第七章的Erica Sadun iPhone Developers Cookbook 3.0 Code Samples。在该示例中,您可以在此行下看到,屏幕上的触摸将存储到NSMutableArray
中,以便稍后使用UIView
方法drawRect
进行绘制。手指绘图效率问题
有了这个解决方案,我有一个问题,当points
数组保持重要的点数。我怎么解决这个问题?如何在不清除UIView
的情况下调用drawRect
方法,并只添加最后一行(在已绘制的行和points
数组的最后一个点之间)?
// Start new array
- (void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event
{
self.points = [NSMutableArray array];
CGPoint pt = [[touches anyObject] locationInView:self];
[self.points addObject:[NSValue valueWithCGPoint:pt]];
}
// Add each point to array
- (void) touchesMoved:(NSSet *) touches withEvent:(UIEvent *) event
{
CGPoint pt = [[touches anyObject] locationInView:self];
[self.points addObject:[NSValue valueWithCGPoint:pt]];
[self setNeedsDisplay];
}
// Draw all points
- (void) drawRect: (CGRect) rect
{
if (!self.points) return;
if (self.points.count < 2) return;
CGContextRef context = UIGraphicsGetCurrentContext();
[current set];
CGContextSetLineWidth(context, 4.0f);
for (int i = 0; i < (self.points.count - 1); i++)
{
CGPoint pt1 = POINT(i);
CGPoint pt2 = POINT(i+1);
CGContextMoveToPoint(context, pt1.x, pt1.y);
CGContextAddLineToPoint(context, pt2.x, pt2.y);
CGContextStrokePath(context);
}
}
感谢您的阅读。
有人可以帮助我吗? – 2011-02-25 13:31:07