可能重复:
how to create an application which allows user to free hand drawing?
我正在尝试在视图上实现自由手写。我使用了一些现有的代码,当用户编写非常慢时,这种代码运行良好,就好像用户写得非常快,它没有识别所有的点(用户触摸的点是什么)。 我的示例代码在这里
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self];
currentPoint.y -= 20;
UIGraphicsBeginImageContext(self.bounds.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
if (mode == DrawingModePen) {
NSLog(@"drawing");
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [_drawingPenColor CGColor]);
}
else {
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor clearColor] CGColor]);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextClearRect (UIGraphicsGetCurrentContext(), CGRectMake(lastPoint.x, lastPoint.y, 20, 20));
CGContextStrokePath(UIGraphicsGetCurrentContext());
NSLog(@"clearing");
}
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
mouseMoved++;
if (mouseMoved == 10) {
mouseMoved = 0;
}
}
还有其他方法可以实现吗?我见过GLPaint由apple提供。它在我的应用程序中引起内存警告也没什么用处。