UIPickerView的“抢先式"讯息? [英] UIPickerView "preemptive" messages?

查看:96
本文介绍了UIPickerView的“抢先式"讯息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到UIPickerView被计算任务饿死"的情况;换句话说,UIPickerView永远不会更新-因此也就不会发送消息-因为正在执行非常繁重的计算任务.选择器控制着计算的各个方面,因此两者必须表现得很好.

I have a situation where my UIPickerView is getting "starved" by a computation task; in other words, the UIPickerView is never updated -- and hence, never sends messages -- because a very heavy compute task is happening. The picker controls aspects of the computation, so the two have to play nice.

我想到了在单独的线程中运行计算.似乎那样会使选择器自由更新.但是,要使我的计算具有多线程能力是一项艰巨的任务,因此我想找到另一个解决方案.

I thought of running the computation in a separate thread. Seems like that would leave the picker free to update. However, it'd be a massive undertaking to make my computation multithread-able, so I'd like to find another solution.

选择器(或其他UI控件)是否可以抢占"代码块的执行?计算是循环的;迭代的数量使它变得繁重.如果选择器甚至可以在某处设置标志,则循环可能会中断,这将与程序的流程一起工作.

Is it possible for a picker (or other UI controls) to "preempt" the execution of a block of code? The computation is in a loop; the number of iterations is what makes it heavy. If the picker could even set a flag somewhere, the loop could break itself, which would work with the flow of the program.

如果循环可以轮询选择器,那也将起作用.但是,我还没有找到一种方法.

If the loop could poll the picker, that would also work. But, I haven't found a way to do that.

想法?

(ps.我昨天发布了一个类似的问题,但并没有真正正确地询问过-当时还不知道是什么问题!)

(ps. I posted a similar question yesterday, but didn't really ask it correctly -- didn't quite know what the problem was at that time!)

推荐答案

我假设您的意思是如果移动了选择器,请设置一个标志.如果是这样,则可以执行此操作-查看选择器委托,并在调用任何一个或所有委托时,设置一个标志.如果您的计算是由另一个或多个其他类完成的,则在已取消"的那些类上创建一个新属性,选择器可以对其进行设置,并在设置结束时进行计算.

I assume you mean by flag that if the picker is moved, set a flag. If so you can do this - look at the picker delegates, and when any or all of them get called, set a flag. If your computation is done by another class or classes, then create a new property on those classes "cancelled", the picker can set it, and when set the computation ends.

在开始另一个计算之前,您将清除该取消标志,然后开始计算.

Before you start another computation you would clear that cancel flag, then kick off the computation.

您还可以在选择器上方放置一个简单的工具栏(常见做法),在该工具栏中您可以使用一些控件来启动计算,显示进度和取消计算.

You can also put a simple toolbar above the picker (common practice) where you have controls that could start the computation, show progress, and cancel it.

如果问题是用户尝试操作时选择器停顿了,则子类UIPicker,拦截触摸事件,并在触摸选择器时取消所有计算.唯一的麻烦是,如果用户旋转"选择器,则需要等待直到它稳定下来,但是您将不知道要等待多长时间.根据最后一次触摸消息,您将必须使用启发式方法来等待didSelectRow:或超时,然后才能重新开始计算.

if the issue is the picker is stuttering when the user is trying to manipulate it, then subclass UIPicker, intercept touch events, and while the picker is being touched, cancel all computations. The only complication is that if the user "spins" the picker, you'd want to wait til it settles, but you would not know how long to wait. Depending on the last touch message, you would have to use a heuristic to wait for didSelectRow: or a timeout before restarting the computation.

这篇关于UIPickerView的“抢先式"讯息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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