构建用于神经科学项目研究目的的“DrawSomething”风格应用程序

时间:2013-01-16 19:15:26

标签: ios objective-c opengl-es core-graphics

我正在尝试实现一个为iPad(4)绘制用户手写(光标位置)的视图。我看到Apple的示例代码,它使用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

问题是,我每次触摸都会画一切(墨水是一个包含两个CGPOINT和一个时间戳的类),过了一段时间,这会大大减慢创建实质性延迟的速度。

我的目标是能够以精确的方式捕捉手写内容并精确播放。

另外需要考虑的是,我正在使用一个提供压力信息的手写笔,所以我需要能够在改变宽度时画出我的线条。

非常感谢任何建议。

1 个答案:

答案 0 :(得分:1)

不是将点存储在数组中,而是将它们存储在数组和UIBezierPath中。然后你只需要在drawRect中绘制bezier路径:而不是设置整个方案。

手写笔不提供压力信息 - 至少在iOS上没有。 iPhone的电容式触摸屏不具有电阻性。更改宽度的标准算法是使用速度作为因子,并使用您填写的小三角形绘制以创建线条。

有趣!以下是相关文章:http://www.nearinfinity.com/blogs/jason_harwig/2012/11/06/capture-a-signature-on-ios.html

相关问题