我有一个UITableViewController默认值UITableView.我开始用手指慢慢拖动桌子滚动,即不用手指轻弹.每次表格在屏幕上移动时,scrollViewDidScroll都会调用控制器的方法; 在满足我指定某些情况下,这些调用的一个scrollViewDidScroll用途performSelector:withObject:afterDelay安排在稍后的时间有所行动.
但是,我发现在我松开手指之前动作才会执行.例如,如果我将afterDelay参数设置为2秒,但是按住我的手指5秒钟,当我松开手指并且动作执行时间太晚时间为3秒.是否有任何方法允许操作(即更新UI,因此必须在主线程中运行)在手指仍在屏幕上时执行?
谢谢!
在Mac OS X上,Cocoa是事件驱动的.我的意思是每个东西都是由UI驱动的 - 用户点击某个东西或移动某个区域会导致调用事件处理程序.Main简单地调用NSApplicationMain(),它会创建一个无限循环,直到程序退出.
那些不是用户驱动的任务呢?我会在哪里放置需要在后台工作的任务来提供程序的基本功能?例如,Photo Booth在用户与UI交互时执行某些操作(即,当用户单击某个按钮时更改为摄像机).但是,Photo Booth还不断从相机中检索帧以提供视频流.
所以我的问题是非UI任务代码在哪里?Main()实际上不是一种可能,因为我需要在程序的整个生命周期中运行任务,而不是在程序刚启动时运行.
我在Mac OS X Snow Leopard上(我想要一些适用于所有Mac系统的东西:即Lion OS X)并在Xcode中使用Obj-C Cocoa应用程序.
我正在开发一个在应用程序的主线程中运行的UI测试框架(KIF-next).基本过程是:
runUntilDate:.这种方法效果非常好.您可以模拟点击事件,您可以在滑块刷卡,观看警示的意见动画进出,scrollToRowAtIndexPath :. 除了一个特定的场景,拖动滚动视图,一切正常.
您可以上下拖动滚动视图,但是当您松开手指时,没有任何反应.此外,触摸事件不再被其他视图识别,您可以做的就是拖动滚动视图.
如果执行模式结束,程序退出,滚动视图将尽自己的弹跳/减速,但这不是一种选择,因为我的OCUnit执行顺序执行我的工作.
我的问题:为什么会发生这种情况以及可以采取哪些措施来纠正它?
我有两个演示:
使用此代码,您可以测试您的应用程序并确认一切正常,直到您的滚动.在那之后,没有任何作用.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
while (YES) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
}
});
return YES;
}
Run Code Online (Sandbox Code Playgroud)
如果您连线该代码到一个按钮,你会看到同样的效果如上,如果你的第一次轻触按钮,然后滚动.计时器完成后,您将看到您的应用程序恢复生机.
- (IBAction)spinButtonClicked:(id)sender
{
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:5]];
}
Run Code Online (Sandbox Code Playgroud) 出于某种原因,当我按下任何控件上的(单击并按住)时,我的应用程序中的NSTimer会冻结,直到我释放鼠标按钮才会触发.我按下鼠标时根本不会触发.这对于短期来说很好,但是如果我打开一个弹出菜单,或者组合框掉落,它也会冻结.
我不确定是否有遗漏的东西,但似乎这是不正确的行为.
我希望能够单击NSPopUpButtonCell的向下箭头(甚至单击并按住NSTableView),而不会冻结整个NSTimer(重绘NSView).
任何意见/建议将不胜感激.
使用模式NSDefaultRunLoopMode将NSTimer添加到currentRunLoop.
NSRunLoop和CFRunLoop中有方法,运行到特定时间CFRunLoopInMode和NSRunLoop - runUntillDateT
这些方法指定运行的时间.
但是,我想在runLoop中处理所有消息,如果完成并且空闲则退出.因为某些runLoop源可以无限期地处理.所以,我不能提及时间.
我正在做一个需要计时器的应用程序。我已经尝试过,NSTimer但NSTimer总是会丢失或延迟。iOS SDK中是否有一个可以准确记录时间的类。20ms到40ms之间的精度是可以的。我希望能够在固定的时间间隔内更改图像。
NSTimer *timer1 = [NSTimer scheduledTimerWithTimeInterval:.04f target:self selector:@selector(timer1) userInfo:nil repeats:YES];
- (void)timer1
{
NSLog(@"timer1 timer %@",[NSDate date]);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试从名为connect的方法中获取URL的内容.它读取其配置设置并尝试获取URL.它似乎需要运行循环来执行.我虽然只执行一次并完成.似乎除了每次获取此URL时,无论何时调用connect,我都不需要运行该循环.有没有更好的方法呢?
- (BOOL) connect
{
// read serverName and eventId from preferences
NSMutableArray* preferences;
preferences = [NSMutableDictionary dictionaryWithContentsOfFile:@"/tmp/wt.plist"];
// if these values aren't nil or blank
if ([preferences valueForKey:@"serverAddress"] && [preferences valueForKey:@"eventId"]) {
[self setServerAddress:@"172.16.28.210"];
[self setEventId:@"OT-3037009"];
} else{
// return some kind of error message
}
NSLog(@"VideoDataURL: %@", [self getVideoDataURL]);
// grab the URL and query the server
NSURL *myURL = [NSURL URLWithString:@"http://localhost"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:2];
__unused NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; …Run Code Online (Sandbox Code Playgroud) 我有一个关于[NSDate distantFuture]的问题,我这样使用它:
while (_connection != nil)
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSData distantFuture]];
}
Run Code Online (Sandbox Code Playgroud)
我收到警告"类方法'+ remoteFuture'未找到(返回类型默认为'id')".我运行代码,出错: - [NSConcreteData timeIntervalSinceReferenceDate]:无法识别的选择器发送到实例0x80d18e0
*终止应用程序由于未捕获的异常'NSInvalidArgumentException',原因是: ' - [NSConcreteData timeIntervalSinceReferenceDate]:无法识别的选择发送到实例0x80d18e0'*第一掷调用堆栈:(0x1e63022 0x1606cd6 0x1e64cbd 0x1dc9ed0 0x1dc9cb2 0x10af3e1 0x72181 0x11300ad 0x18a9330 0x18aa439 0x95707b24 0x957096fe)终止叫投掷一个例外.我使用[NSDate dateWithTimeIntervalSinceNow:2],错误是一样的.有人能帮助我吗?多谢.
我对NSRunLoop实现空闲/休眠线程的能力感兴趣,而没有强烈的CPU使用.如何在Objective-c中实现这一目标?消耗高CPU的天真解决方案是(YES){...}
我有以下代码,我没有得到我预期的结果.
#import "CancelPerformSelectorTestAppDelegate.h"
@implementation CancelPerformSelectorTestAppDelegate
@synthesize window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window makeKeyAndVisible];
for(unsigned int i = 0; i < 10; i++){
NSTimeInterval waitThisLong = i;
[self performSelector:@selector(foo) withObject:nil afterDelay: waitThisLong];
}
[[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget: self];
return YES;
}
- (void) foo {
static unsigned int timesCalled = 0;
++timesCalled;
NSLog(@"%s: I am called for the %d-st/nd/th time", __func__, timesCalled);
}
- (void)applicationWillResignActive:(UIApplication *)application {}
- (void)applicationDidBecomeActive:(UIApplication *)application {}
- (void)applicationWillTerminate:(UIApplication *)application {}
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {}
- …Run Code Online (Sandbox Code Playgroud) 我有一个有NSTimer的类,当我发布它时,也会失效吗?或者我可以在dealloc中执行它们:
- (void)dealloc
{
[_timer invalidate];
[_timer release];
[super dealloc];
}
Run Code Online (Sandbox Code Playgroud)
我在这个帖子中读到:
选择的答案说invalidate方法也会释放,所以如果我使它失效,我不需要释放它?
谢谢!
如何记录Objective-C事件循环的单次迭代中发送的每条消息?
我希望进一步了解Objective-C运行时,并认为这将是一个良好的开端.
我有点困惑是否NSRunLoop是thread safe or not.所以我需要澄清一下它是否安全,为什么?
任何帮助都很明显.
nsrunloop ×13
objective-c ×7
ios ×6
nstimer ×3
cocoa ×2
iphone ×2
cocoa-touch ×1
event-driven ×1
event-loop ×1
macos ×1
nsdate ×1
operation ×1
uikit ×1
uitableview ×1