首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为神经科学项目研究构建“DrawSomething”风格的应用程序

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

Stack Overflow用户
提问于 2013-01-17 03:15:26
回答 1查看 164关注 0票数 2

我正在尝试实现一个为iPad (4)绘制用户手写(光标位置)的视图。我看到了苹果公司的示例代码,它使用了OpenGL,然而,有些部分我无法理解,所以我尝试使用核心图形来实现它。

代码语言:javascript
复制
    #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和一个时间戳的类),过了一段时间后,这会戏剧性地减慢速度,造成很大的滞后。

我的目标是能够以精确的方式捕获笔迹,并精确地回放它。

另一件要考虑的事情是,我使用的是一支能给出压力信息的手写笔,所以我需要能够在变化的宽度中绘制我的线条。

我们将非常感谢您的任何建议。

EN

回答 1

Stack Overflow用户

发布于 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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14365995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档