我正在尝试实现一个为iPad (4)绘制用户手写(光标位置)的视图。我看到了苹果公司的示例代码,它使用了OpenGL,然而,有些部分我无法理解,所以我尝试使用核心图形来实现它。
#import "PaintView.h"
#include <stdlib.h>
@implementation PaintView
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if(self) {
//
pointsToDraw = [[NSMutableArray alloc] init];
}
return self;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
NSLog(@"%@", touch);
CGPoint location = [touch locationInView:self];
CGPoint previousLocation = [touch previousLocationInView:self];
Ink *ink = [[Ink alloc] initWithPoint:previousLocation toPoint:location time:touch.timestamp];
// UITouch *newTouch = [touch copy];
[pointsToDraw addObject:ink];
[self setNeedsDisplay];
}
- (void)drawLine:(CGPoint)startingPoint toPoint:(CGPoint)endingPoint context:(CGContextRef)context
{
// Drawing code
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGRect temp = CGRectMake(10, 10, 100, 100);
// Draw them with a 2.0 stroke width so they are a bit more visible.
CGContextSetLineWidth(context, 2.0);
CGContextMoveToPoint(context, startingPoint.x,startingPoint.y); //start at this point
CGContextAddLineToPoint(context, endingPoint.x, endingPoint.y); //draw to this point
// and now draw the Path!
}
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
// [self drawLine:CGPointMake(10, 10) toPoint:CGPointMake(30, 30)];
CGContextRef context = UIGraphicsGetCurrentContext();
for (Ink *ink in pointsToDraw){
[self drawLine:ink.point toPoint:ink.previousPoint context:context];
}
CGContextStrokePath(context);
}
@end问题是,每次触摸我都会绘制所有内容(ink是一个包含两个CGPOINT和一个时间戳的类),过了一段时间后,这会戏剧性地减慢速度,造成很大的滞后。
我的目标是能够以精确的方式捕获笔迹,并精确地回放它。
另一件要考虑的事情是,我使用的是一支能给出压力信息的手写笔,所以我需要能够在变化的宽度中绘制我的线条。
我们将非常感谢您的任何建议。
发布于 2013-01-17 04:09:39
不是将点存储在数组中,而是将它们存储在数组和UIBezierPath中。然后,您只需要在drawRect中绘制bezier路径:而不是设置整个方案。
手写笔不能给出压力信息--至少在iOS上不能。iPhones的触摸屏是电容式的,没有电阻性。更改宽度的标准算法是使用速度作为因子,并使用填充的小三角形来绘制线条。
有趣的!这里有一篇相关文章:http://www.nearinfinity.com/blogs/jason_harwig/2012/11/06/capture-a-signature-on-ios.html
https://stackoverflow.com/questions/14365995
复制相似问题