不能画得足够快以跟上touchesMoved? [英] can't draw fast enough to keep up with touchesMoved?

查看:84
本文介绍了不能画得足够快以跟上touchesMoved?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的iPhone应用程序中实现简单的绘图功能。我尝试使用位图画笔更新位图,我也尝试了本教程

I am trying to implement simple paint functionality in my iPhone app. I tried updating a bitmap with a bitmap brush, and I also tried this tutorial.

两种方法都有同样的问题,即使代码几乎完全不同。它只发生在设备上 - 模拟器工作正常。

Both methods have the same problem, even though the code is almost totally different. It happens only on the device - the simulator works fine.

当我触摸屏幕并移动手指时,屏幕不会更新。当我暂停或抬起手指时,屏幕会更新。这不是一个非常好的用户体验!

When I touch the screen and move my finger, the screen does not get updated. When I pause or lift my finger, then the screen gets updated. This is not a very good user experience!

我尝试直接从touchesMoved调用drawRect,但发现绘图上下文(我使用UIGraphicsGetCurrentContext检索)对于许多调用都是无效的,所以自己绘制屏幕每个touchesMoved都不起作用。

I tried calling drawRect from touchesMoved directly, but found that the drawing context (which I retrieve using UIGraphicsGetCurrentContext) is invalid for many of the calls, so painting the screen myself for every touchesMoved doesn't work.

任何想法?

感谢您的帮助,这一直非常令人沮丧!

Thanks for any help, this has been quite frustrating!

Henning

推荐答案

听起来好像你不是为主运行循环提供更新显示的机会。您的绘图代码执行时间可能比触摸事件之间的时间长,因此显示永远不会更新。当你抬起手指时,它会进行更新,因为它不再受到你的绘图负担。

It sounds to me like you're not giving the main run loop a chance to update the display. Your drawing code may be taking longer to execute than the time between touch events, so the display is never updated. When you lift your finger, it does the updating because it's no longer burdened with your drawing.

你可能会考虑优化你的绘图以加快它的速度(仅在脏的范围内绘图)例如,屏幕的区域,使用NSOperationQueue之类的东西来排队绘图的繁重计算以在后台线程上运行,或选择性地删除触摸绘图事件以保持响应平滑。

You might consider optimizing your drawing to speed it up (drawing only within the dirty region of the screen, for example), using something like NSOperationQueue to queue up the heavy calculations of your drawing to run on a background thread, or selectively dropping touch drawing events to keep your response smooth.

另一种可能性是将重绘图代码放在一个单独的方法中,并通过performSelector:withObject:afterDelay调用它,延迟为10毫秒(或更小)。这可能会使主运行循环有机会以其当前状态更新显示。我没有测试过这个,但如果我没记错的话,我已经看过这个工作了。

One additional possibility is placing your heavy drawing code in a separate method and calling it via performSelector:withObject:afterDelay, with a 10 millisecond (or smaller) delay. This might give the main run loop a chance to update the display with its current state. I haven't tested this, but if I remember correctly I've seen this work.

这篇关于不能画得足够快以跟上touchesMoved?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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