【问题标题】:Building 'DrawSomething' style app for neuroscience project research purposes为神经科学项目研究目的构建“DrawSomething”风格的应用程序
【发布时间】:2013-01-16 19:15:25
【问题描述】:

我正在尝试实现一个为 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 和一个时间戳的类),一段时间后,这会大大减慢速度,从而产生大量延迟。

我的目标是既能以精确的方式捕捉笔迹,又能精确地回放。

要考虑的另一件事是,我使用的是提供压力信息的触控笔,因此我需要能够在改变宽度时画出我的线条。

任何建议将不胜感激。

【问题讨论】:

  • 如果您还没有,请从 WWDC 2012 会议 506 中获取视频 - 优化 2D 图形和动画性能。这正是您在此处尝试的,它们展示了如何通过一系列优化获得平滑的绘图性能。

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


【解决方案1】:

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

触控笔不提供压力信息 - 至少在 iOS 上不提供。 iPhone 的电容式触摸屏不是电阻式的。更改宽度的标准算法是使用速度作为因素,并使用您填充的小三角形来绘制线条。

好笑!这是一篇相关文章:http://www.nearinfinity.com/blogs/jason_harwig/2012/11/06/capture-a-signature-on-ios.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 2014-04-20
    • 2020-12-06
    • 1970-01-01
    相关资源
    最近更新 更多