我想开发一个应用程序来绘制触摸 iPhone 屏幕并搜索如何做我在 Erica Sadun iPhone 开发人员手册的第八章中找到了一个示例配方 (09-Paint) 3.0 Code Samples .在示例中,正如您在这行下方看到的那样,屏幕上的触摸被存储到 NSMutableArray
中,稍后使用 UIView
方法 drawRect绘制
.
使用此解决方案时,当 points
数组保留大量点时,我会遇到问题。我怎么解决这个问题?如何调用 drawRect
方法而不清除 UIView
并仅添加最后一行(在已绘制的线和 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);
}
}
感谢阅读。
最佳答案
现在一切正常。 SkylarEC 有很棒的教程 here ,他解释了如何解决这个问题。感谢阅读。
关于objective-c - 手指画的效率问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5111962/