我正在尝试实现一个为 iPad (4) 绘制用户笔迹(光标位置)的视图。我看到了使用 OpenGL 的 Apple 示例代码,但是,有些部分我无法理解,所以我尝试使用核心图形来实现它。
#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 和一个时间戳的类),过了一段时间,这大大减慢了速度,从而产生了大量的滞后。
我的目标是既能以精确的方式捕捉笔迹,又能精确地回放。
另一件需要考虑的事情是,我使用的是提供压力信息的手写笔,所以我需要能够在改变宽度时画出我的线条。
任何建议将不胜感激。