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