阅读背景:

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

来源:互联网 

I am trying to implement a view that draws the users handwriting (curser position) for the iPad (4). I saw Apple's sample code, that uses OpenGL, however, there were parts I couldn't understand, so, I tried implementing this using 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

The problem is, every touch I draw everything (ink is a class that contains two CGPOINT's and a time stamp), and after a while this dramatically slows things down creating substantial lag.

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

My goal is to be able to both capture handwriting in a precise way, and play it back precisely.

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

Another things to consider, is that I am using a stylus which gives pressure information so I need to be able to draw my line in changing widths.

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

Any advice will be greatly appreciated.

任何建议将不胜感激。

1 个解决方案

#1


1  

Instead of storing the points in an array, store them in an array and a UIBezierPath. Then you only need to draw the bezier path within drawRect: instead of setting up the whole scheme.

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

The stylus doesn't give pressure information - at least not on iOS. iPhones have capacitive touch screens not resistive. The standard algorithm to change width is to use the speed as the factor and draw using little triangles that you fill in to create your line.

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

Funny! Here is a relevant article: https://www.nearinfinity.com/blogs/jason_harwig/2012/11/06/capture-a-signature-on-ios.html

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


分享到: