iPhone应用中如何使用三轴感应器代码案例

移动开发 iOS
iPhone应用中如何使用iPhone三轴感应器代码案例是本文要介绍的内容你,主要是来认识并学习iphone应用中的三轴感应器。来考哪详细内容。

iPhone应用中如何使用iPhone三轴感应器代码案例是本文要介绍的内容你,主要是来认识并学习iphone应用中的三轴感应器。很多应用里都要用到苹果iPhone自带的三轴感应器(加速度传感器),希望下面这段代码例子能为您使用iPhone三轴感应器打开思路。如图所示:

iPhone应用中如何使用三轴感应器代码案例 

  1. #include <IOKit/IOKitLib.h> 
  2. #include <CoreFoundation/CoreFoundation.h> 
  3.  
  4. typedef struct {} *IOHIDEventSystemRef;  
  5. typedef struct {} *IOHIDEventRef;  
  6. float IOHIDEventGetFloatValue(IOHIDEventRef ref, int param);  
  7.  
  8. void handleHIDEvent(int a, int b, int c, IOHIDEventRef ptr) {  
  9. int type = IOHIDEventGetType(ptr);  
  10. if (type == 12) {  
  11. float x,y,z;  
  12. x = IOHIDEventGetFloatValue(ptr, 0xc0000);  
  13. y = IOHIDEventGetFloatValue(ptr, 0xc0001);  
  14. z = IOHIDEventGetFloatValue(ptr, 0xc0002);  
  15. // do whatever you need to do with the gravity  
  16. ballSetAccel(x, y);  
  17. }  
  18. }  
  19. #define expect(x) if(!x) { printf("failed: %sn", #x); return; }  
  20. void initialize(int hz) {  
  21. mach_port_t master;  
  22. expect(0 == IOMasterPort(MACH_PORT_NULL, &master));  
  23.  
  24. int page = 0xff00usage = 3;  
  25.  
  26. CFNumberRef nums[2];  
  27. CFStringRef keys[2];  
  28. keys[0] = CFStringCreateWithCString(0, "PrimaryUsagePage", 0);  
  29. keys[1] = CFStringCreateWithCString(0, "PrimaryUsage", 0);  
  30. nums[0] = CFNumberCreate(0, kCFNumberSInt32Type, &page);  
  31. nums[1] = CFNumberCreate(0, kCFNumberSInt32Type, &usage);  
  32. CFDictionaryRef dict = CFDictionaryCreate(0, (const void**)keys,  
  33. (const void**)nums, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);  
  34.  expect(dict);  
  35. IOHIDEventSystemRef sys = (IOHIDEventSystemRef) IOHIDEventSystemCreate(0);  
  36. expect(sys);  
  37.  CFArrayRef srvs = (CFArrayRef)IOHIDEventSystemCopyMatchingServices(sys, dict, 0, 0, 0);  
  38. expect(CFArrayGetCount(srvs)==1);  
  39.    
  40. io_registry_entry_t serv = (io_registry_entry_t)CFArrayGetValueAtIndex(srvs, 0);  
  41. expect(serv);  
  42.    
  43. CFStringRef cs = CFStringCreateWithCString(0, "ReportInterval", 0);  
  44. int rv = 1000000/hz;  
  45. CFNumberRef cn = CFNumberCreate(0, kCFNumberSInt32Type, &rv);  
  46.    
  47. int res = IOHIDServiceSetProperty(serv, cs, cn);  
  48. expect(res == 1);  
  49.    
  50. res = IOHIDEventSystemOpen(sys, handleHIDEvent, 0, 0);  
  51. expect(res != 0);  

小结:iPhone应用中如何使用三轴感应器代码案例的内容介绍完了,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: starming社区
相关推荐

2012-07-11 09:21:35

Windows Pho

2011-08-18 16:42:07

iPhone应用APNS推送

2011-08-15 15:44:46

iPhone开发PDF

2011-08-18 16:24:44

iPhone开发图片

2011-08-17 15:10:21

iPhone开发Web视图

2011-08-16 15:48:37

iPhone开发抓图程序

2013-05-21 11:24:07

Android游戏开发Sensor重力感应

2011-08-19 14:34:03

iPhone开发

2011-08-19 10:05:30

iPhone开发

2011-08-19 17:02:46

iPhone开发

2011-08-18 16:03:34

iPhone上传图片

2011-08-08 13:50:29

iPhone开发 NSOperatio 多线程

2011-08-10 16:08:02

iPhoneProtocol协议

2011-08-15 11:23:41

iPhone开发循环滚动UIScrollVie

2010-11-05 13:02:58

内存iPhone

2011-08-18 15:40:20

iPhone文本切页

2012-05-28 15:55:47

XNA 重力感应

2011-08-09 09:57:59

iPhone服务器Socket

2011-08-08 14:07:49

iPhone开发 字体

2011-08-03 17:27:40

iPhone UIScrollVi
点赞
收藏

51CTO技术栈公众号