我有一个Xcode 5 /可可程序,指定的时间间隔后点击鼠标左键指定的次数。这部分工作正常。当我想要提前停止while循环时会出现问题。主while循环没有得到全局变量的当前值
我正在使用监听程序在程序运行期间检测到任何按键,设置一个stopnow
变量并检查while loop
中的变量。但是,在循环结束之前,while loop
不检测变量中的更改。
此外,我在窗口的标题栏中更改计数器以显示已完成的点击次数,并且在循环结束前不会更新。
当我按下某个键时,确实收到了NSLog
消息。
我很困惑。
我的代码是在这里:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
[[self myWindow] setLevel:NSFloatingWindowLevel];
[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent *event) {
keychar = (unichar) event.characters;
[NSApp activateIgnoringOtherApps:YES];
stopnow = 1;
NSLog(@"Key Pressed = x%x (%x) (%x)",keychar,(keychar&0x7f00),((keychar&0xff00)>>8));
}];
}
- (IBAction)setClickPoint:(NSButton *)sender {
sleep(5);
CGEventRef ourEvent = CGEventCreate(NULL);
cgPoint = CGEventGetLocation(ourEvent);
myPoint = [NSString stringWithFormat:@" (%5.0f,%5.0f)", cgPoint.x, cgPoint.y];
myNewTitle = [mytitle stringByAppendingString:myPoint];
[[self myWindow] setTitle:myNewTitle];
}
(IBAction)strtButton:(NSButton *)sender {
NSLog(@"Entered strButtn");
numClicks = [_nClicks intValue];
numWait = [_nWait floatValue];
i = 0;
while (i < numClicks || numClicks == 0) {
i++;
myTotal = [NSString stringWithFormat:@" %i of %i", i, numClicks];
myNewTitle = [mytitle stringByAppendingString:myPoint];
myNewTitle = [myNewTitle stringByAppendingString:myTotal];
[[self myWindow] setTitle:myNewTitle];
CGWarpMouseCursorPosition(cgPoint);
CGEventRef down = CGEventCreateMouseEvent(0, kCGEventLeftMouseDown,cgPoint, 0);
CGEventPost(kCGSessionEventTap, down);
CFRelease(down);
CGEventRef up = CGEventCreateMouseEvent(0, kCGEventLeftMouseUp,cgPoint, 0);
CGEventPost(kCGSessionEventTap, up);
CGRealease(up);
NSLog(@"stopnow = %i", stopnow);
if (stopnow == 1) {
stopnow = 0;
break;
}
usleep((unsigned int)(numWait * 1000000.0));
}
}
那个低调的匿名懦夫会喜欢解释为什么吗? – Droppy
所以 - 你是说我需要使循环另一种方法,并创建一个线程与该方法?如果是这样,新线程将如何通知主线程它已完成?也许一个回调方法? – user3581648
是的,任何UI更新都可以通过GCD完成,调度到主队列。 – Droppy