2011-02-24 32 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,他解釋瞭如何解決這個問題。謝謝閱讀。