2011-02-24 126 views
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); 
    } 
} 

感谢您的阅读。

+0

有人可以帮助我吗? – 2011-02-25 13:31:07

回答

4

现在它的工作正确。 SkylarEC有一个很好的教程here,他解释了如何解决这个问题。谢谢阅读。