你可以在中使用IOHID的东西
物联网工具包
以获得x,y坐标。
#include <IOHIDEventSystem.h>
创建IOHIDEventSystemClient:
void *ioHIDEventSystem = IOHIDEventSystemClientCreate(kCFAllocatorDefault);
寄存器回调:
IOHIDEventSystemClientScheduleWithRunLoop(ioHIDEventSystem, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
IOHIDEventSystemClientRegisterEventCallback(ioHIDEventSystem, handle_event, NULL, NULL);
注销回调:
IOHIDEventSystemClientUnregisterEventCallback(ioHIDEventSystem, handle_event, NULL, NULL);
IOHIDEventSystemClientUnscheduleWithRunLoop(ioHIDEventSystem, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
回调:
void handle_event (void* target, void* refcon, IOHIDServiceRef service, IOHIDEventRef event) {
if (IOHIDEventGetType(event)==kIOHIDEventTypeDigitizer){
IOHIDFloat x=IOHIDEventGetFloatValue(event, (IOHIDEventField)kIOHIDEventFieldDigitizerX);
IOHIDFloat y=IOHIDEventGetFloatValue(event, (IOHIDEventField)kIOHIDEventFieldDigitizerY);
int width = [[UIScreen mainScreen] bounds].size.width;
int height = [[UIScreen mainScreen] bounds].size.height;
NSLog(@"click : %f, %f", x*width, y*height) ;
}
}
此外,您还可以查看:
IOHIDEventSystemCreate on iOS6 failed
.
希望这能有所帮助。
编辑:请查看日志中的结果。在iPhone 4和5上进行了测试。
void handle_event (void* target, void* refcon, IOHIDServiceRef service, IOHIDEventRef event) {
NSLog(@"handle_event : %d", IOHIDEventGetType(event));
if (IOHIDEventGetType(event)==kIOHIDEventTypeDigitizer){
IOHIDFloat x=IOHIDEventGetFloatValue(event, (IOHIDEventField)kIOHIDEventFieldDigitizerX);
IOHIDFloat y=IOHIDEventGetFloatValue(event, (IOHIDEventField)kIOHIDEventFieldDigitizerY);
NSLog(@" x %f : y %f", x, y);
//2013-03-28 10:02:52.169 MyIOKit[143:907] handle_event : 11
//2013-03-28 10:02:52.182 MyIOKit[143:907] x 0.766754 : y 0.555023
}
}