Dov*_*Dov 0 macos mouse events cocoa objective-c
完全重写
我现在重现了我之前在一个简单的示例项目中发布的问题,所以我完全不知道我可能做错了什么.这就是我现在正在做的事情.
用以下内容替换MyImageView.m的内容:
@interface MyImageView :NSImageView {} @end
@implementation MyImageView
- (void)mouseDown:(NSEvent *)event {
NSLog( @"mouse down event: %@", event );
NSPoint point = [event locationInWindow];
NSLog( @"mouseDown location: (%d,%d)", point.x, point.y );
}
@end
Run Code Online (Sandbox Code Playgroud)在Interface Builder中打开MainMenu.xib
MyImageView
然后我在控制台上看到以下消息:
2011-01-01 13:58:12.351 TestApp[1167:903] mouse down event: NSEvent: type=LMouseDown loc=(237,242) time=2033.7 flags=0x100 win=0x0 winNum=573 ctxt=0x0 evNum=286 click=1 buttonNumber=0 pressure=1
2011-01-01 13:58:12.353 TestApp[1167:903] mouseDown location: (-2057547688,16)
为什么NSEvent的字符串表示中的"loc"是正确的,但-locationInWindow
显然是错误的?我怎么可能会导致这些琐碎的代码错误?
我已经重新启动了我的系统以防可能有所帮助.
这两个元素NSPoint
是浮点数,因此你的NSLog语句是错误的,而不是返回的值.您需要确保格式字符串中的类型与变量的类型匹配,您可以使用强制转换:
这将有效:
NSLog( @"mouseDown location: (%d,%d)", (int) point.x, (int) point.y );
Run Code Online (Sandbox Code Playgroud)
就像这样:
NSLog( @"mouseDown location: (%f,%f)", (float) point.x, (float) point.y );
Run Code Online (Sandbox Code Playgroud)
NSPoint是否包含32位或64位浮点在OS版本和系统之间有所不同.使用内置的点到字符串函数可能是值得的:
NSLog( @"mouseDown location: %@", NSStringFromPoint(point) );
Run Code Online (Sandbox Code Playgroud)
(请注意,实际进行数学和比较时,如果不插入强制转换,它们将自动正常工作.只有NSLog需要帮助才能正确完成.)
归档时间: |
|
查看次数: |
1257 次 |
最近记录: |