【问题标题】:iOS - dispatcherTimer blocking touches events?iOS - dispatcherTimer 阻塞触摸事件?
【发布时间】:2012-06-26 20:37:15
【问题描述】:

我正在使用调度程序源计时器以不同的帧速率更新视图。 (8、12 或 24 FPS)

这是初始化 dispatcherTimer 的代码和用于创建计时器的函数。
(这个函数直接取自apple doc“创建定时器”小节:http://developer.apple.com/library/mac/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/GCDWorkQueues/GCDWorkQueues.html

呼叫:

    self.dispatchTimer = [self createDispatchTimerWithInterval:self.project.frameDuration * NSEC_PER_SEC
                                                    leeway:0.0 * NSEC_PER_SEC
                                                     queue:dispatch_get_main_queue()
                                                     block:displayFrame];

功能:

- (dispatch_source_t)createDispatchTimerWithInterval:(uint64_t)interval leeway:(uint64_t)leeway queue:(dispatch_queue_t)queue block:(dispatch_block_t)block {
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
                                                 0, 0, queue);
    if (timer) {
        dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), interval, leeway);
        dispatch_source_set_event_handler(timer, block);
        dispatch_resume(timer);
    }
    return timer;
}

我的视图完美更新,但没有捕获到触摸事件。我的第一个赌注是“displayFrame”块需要太多处理时间,因为如果我将 frameDuration 减少到 0.5 秒左右,触摸事件就会被捕获。

我只在 iOS 4 上用 iPad 2 测试过这个。

任何帮助或提示将不胜感激!

艾蒂安

更新

我在苹果开发者论坛上问过类似的问题,我得到的答案是:https://devforums.apple.com/thread/156633?tstart=0

【问题讨论】:

  • 使用“时间分析器”在 Instruments 中运行代码,并检查“所有线程状态”选项。当结果回来时,看看“线程策略”。 Instruments 告诉你主线程在做什么?是忙着计算还是等待着什么?

标签: ios input grand-central-dispatch dispatch


【解决方案1】:

主运行循环在每次通过运行循环后排空主队列。当你说你的持续时间太短时,我认为你是对的。如果源向队列中添加新块的速度快于它们被排空的速度,我当然希望 runloop 永远不会恢复处理事件(因为它一直在尝试排空队列)。

【讨论】:

    猜你喜欢
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 2016-03-27
    • 2019-02-17
    • 2015-02-06
    • 2013-04-16
    相关资源
    最近更新 更多