优化iPhone SDK的绘图(带手指触摸)应用程序 [英] Optimizing a drawing (with finger touches) application for iPhone SDK

查看:128
本文介绍了优化iPhone SDK的绘图(带手指触摸)应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用手指绘制简单图表的应用程序。我大部分都在工作,但现在我正在努力优化其性能。当用户快速滑动手指时,我无法捕获足够的触摸事件来绘制平滑的路径。

I'm writing an application that uses your finger to draw simple diagrams. I have it working for the most part but now I'm trying to optimize its performance. When the user swipes their finger fast, I can't capture enough touch events to draw a smooth path.

这是我目前的做法:

1)我将UIView子类化并为CGLayer添加了一个poroperty(获取)懒惰创建,与我的UIView大小相同)。
2)我的UIView子类通过在实例变量中存储当前和最后的触摸点来响应触摸事件。
3)我的视图的setNeedsDisplay被调用,并且在绘制rect中,执行以下操作:
- 从上一个触摸位置绘制一条线到当前触摸位置到CGLayer
- 绘制整个CGLayer一次到我的观点上下文

1) I subclassed a UIView and added a poroperty to a CGLayer (gets created lazily and is the same size as my UIView). 2) My UIView subclass responds to touch events by storing the current and last touch points in instance variables. 3) My view's setNeedsDisplay is called and in the draw rect , do the following: - draw a line from the previous touch location to the current touch location to the CGLayer - draw the entire CGLayer to my views context in one go

主要问题是当用户快速滑动时我得到的触摸事件相对较少,因此我在触摸之间绘制的线很长使路径看起来不平滑。

The main problem is when a user swipes fast I get relatively few touch events, so the lines I draw between the touches are long and makes the path look jagged not smooth.

我的问题:

1)drawRect(在我的UIView子类上) )和我的UIView子类上的触摸事件处理程序在同一个线程中被调用?即我可以执行线程(一个在触摸事件中,第二个在我的绘制rect中)?

1) Does drawRect (on my UIView subclass) and my touch event handlers on my UIView subclass get called in the same thread? I.e. could I have to threads executing (one in a touch event and the second in my draw rect)?

如果没有 - 在调用drawRect时,触摸事件是否排队?我如何才能提高性能 - 只需提高drawRect的性能?

If no - do touch events get queued up while drawRect is being called? And how can I improve performance - simply improve performance of drawRect?

如果是 - 我怎样才能获得更多触摸事件,以便我可以画出更平滑的路径?

If yes - how can I get more touch events to happen so I can draw a smoother path?

谢谢。

推荐答案

另一种方法是在两者之间插入曲线样本点。当手指拖动开始时,开始收集样本点。随着点数的增加,重新划线。用两个点绘制一条直线,用三条或更多条绘制一条曲线。当采样位于定义距离内的两个点时,您可以重新启动该过程。这将允许您在一个动作中绘制两个弧(如m) - 当您改变方向时,您自然会在中间停顿,可能足够长到两个或更多样本。

Another approach is to interpolate the curve between the sample points. When the finger drag starts, begin collecting sample points. As the number of points increase, redraw the line. With two points, draw a straight line, with three or more draw a curve. You can re-start the process when two points are sampled that lie within a defined distance. This would allow you to draw two arcs (like a 'm') in one motion - you naturally pause in the middle as you change direction, possibly long enough for two or more samples.

这篇关于优化iPhone SDK的绘图(带手指触摸)应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆