传感器,加速计,蓝牙以及UIDevice方法的详细说明

我的博客:http://blog.csdn.net/nsydianzi


距离传感器:

关于距离传感器使用很简单,流程也少.

步骤:

第一步:打开举例传感器;(获取当前设备,并且将当前设备的proximityMonitoringEnabled属性值设置为YES)

第二步:监听传感器的变化;(利用通知,当距离传感器前方有物体接近时会发送一个通知,离开时也会发通知UIDeviceProximityStateDidChangeNotification是定义的一个常量)

 
 
  1. // 1.开启距离传感器(注意: 默认情况距离传感器是关闭的)

  2. // 只要开启之后, 就开始实时监听

  3. [UIDevice currentDevice].proximityMonitoringEnabled = YES;

  4. // 2.当监听到有物体靠近设备时系统会发出通知

  5. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChange:) name:UIDeviceProximityStateDidChangeNotification object:nil];

  6. // 当监听到有物体靠近设备时调用

  7. - (void)proximityStateDidChange:(NSNotification *)note

  8. {

  9.   if( [UIDevice currentDevice].proximityState)//判断物体接近状态

  10.   {

  11.       NSLog(@"有物体靠近");

  12.   }else

  13.   {

  14.       NSLog(@"物体离开");

  15.   }

  16. }


加速度传感器:


在iOS4以前使用UIAccelerometer来获取到设备的加速度,用法简单,但是在iOS5时已经过期.

但是从iOS4开始苹果提供了一个库,CoreMotion.framework来获取设备的加速度信息,功能更加强大

iOS8已经完全不能使用

UIAccelerometer使用步骤:    //被废弃,简单写一点用法步骤 

1.获得单例对象

UIAccelerometer *acc = [UIAccelerometer sharedAccelerometer];

2.设置代理

遵循<UIAccelerometerDelegate>协议

acc.delegate = self;

实现代理方法

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration;

这个方法只要采集到数据就会调用

3.设置采样间隔

acc.updateInterval = 1 / 30;


CoreMotion使用步骤:

1.创建运动管理者对象

self.mgr = [[CMMotionManager allocinit];

2.判断加速计是否可用(最好是要判断)

if (self.mgr.isAccelerometerAvailable) {}

判断加速计可用则设置下面的数据i

3.设置采样间隔

self.mgr.accelerometerUpdateInterval = 0.1 ;/// 30.0;

4.开始采样

[self.mgr startAccelerometerUpdatesToQueue:[NSOperationQueuemainQueuewithHandler:^(CMAccelerometerData*accelerometerData, NSError *error) {

            // 这个block是采集到数据时就会调用

            if (error) return ;

            CMAcceleration acceleration = accelerometerData.acceleration;

            NSLog(@"x = %f y = %f z = %f", acceleration.x, acceleration.y , acceleration.z);

            self.label.text = [NSString stringWithFormat:@"x = %.2f y = %.2f z = %.2f",acceleration.x, acceleration.y , acceleration.z];

        }];



/*

         isAccelerometerActive 是否正在采集

         accelerometerData 采集到得数据

         startAccelerometerUpdates  pull

         startAccelerometerUpdatesToQueue  push

         stopAccelerometerUpdates 停止采集

         accelerometerUpdateInterval 采样时间

*/




 
 
  1. typedef NS_ENUM(NSInteger, UIDeviceOrientation) //设备方向

  2. {

  3.    UIDeviceOrientationUnknown,

  4.    UIDeviceOrientationPortrait,                   // 竖向,头向上

  5.    UIDeviceOrientationPortraitUpsideDown,  // 竖向,头向下

  6.    UIDeviceOrientationLandscapeLeft,         // 横向,头向左

  7.    UIDeviceOrientationLandscapeRight,       // 横向,头向右

  8.    UIDeviceOrientationFaceUp,                   // 平放,屏幕朝下

  9.    UIDeviceOrientationFaceDown                // 平放,屏幕朝下

  10. };

  11. typedef NS_ENUM(NSInteger, UIDeviceBatteryState) //电池状态

  12. {

  13.    UIDeviceBatteryStateUnknown,

  14.    UIDeviceBatteryStateUnplugged,   // 未充电

  15.    UIDeviceBatteryStateCharging,     // 正在充电

  16.    UIDeviceBatteryStateFull,             // 满电

  17. };

  18. typedef NS_ENUM(NSInteger, UIUserInterfaceIdiom) //用户界面类型

  19. {

  20. //iOS3.2以上有效

  21. #if __IPHONE_3_2 <= __IPHONE_OS_VERSION_MAX_ALLOWED

  22.    UIUserInterfaceIdiomPhone,           // iPhone 和 iPod touch 风格

  23.    UIUserInterfaceIdiomPad,              // iPad 风格

  24. #endif

  25. };

  26. #define UI_USER_INTERFACE_IDIOM() ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? [[UIDevice currentDevice] userInterfaceIdiom] : UIUserInterfaceIdiomPhone)

  27. #define UIDeviceOrientationIsPortrait(orientation)  ((orientation) == UIDeviceOrientationPortrait || (orientation) == UIDeviceOrientationPortraitUpsideDown)

  28. #define UIDeviceOrientationIsLandscape(orientation) ((orientation) == UIDeviceOrientationLandscapeLeft || (orientation) == UIDeviceOrientationLandscapeRight)

  29. NS_CLASS_AVAILABLE_IOS(2_0) @interface UIDevice : NSObject {

  30. @private

  31.    NSInteger _numDeviceOrientationObservers;

  32.    float     _batteryLevel;

  33.    struct {

  34. unsigned int batteryMonitoringEnabled:1;

  35. unsigned int proximityMonitoringEnabled:1;

  36. unsigned int expectsFaceContactInLandscape:1;

  37.        unsigned int orientation:3;

  38.        unsigned int batteryState:2;

  39.        unsigned int proximityState:1;

  40.    } _deviceFlags;

  41. }

  42. + (UIDevice *)currentDevice; // 获取当前设备

  43. @property(nonatomic,readonly,retain) NSString    *name;                // e.g. "My iPhone"

  44. @property(nonatomic,readonly,retain) NSString    *model;               // e.g. @"iPhone", @"iPod touch"

  45. @property(nonatomic,readonly,retain) NSString    *localizedModel;    // localized version of model

  46. @property(nonatomic,readonly,retain) NSString    *systemName;      // e.g. @"iOS"

  47. @property(nonatomic,readonly,retain) NSString    *systemVersion;    // e.g. @"4.0"

  48. @property(nonatomic,readonly) UIDeviceOrientation orientation;       // 除非正在生成设备方向的通知,否则返回UIDeviceOrientationUnknown 。

  49. @property(nonatomic,readonly,retain) NSUUID      *identifierForVendor NS_AVAILABLE_IOS(6_0);      // 可用于唯一标识该设备,同一供应商不同应用具有相同的UUID 。

  50. @property(nonatomic,readonly,getter=isGeneratingDeviceOrientationNotifications) BOOL generatesDeviceOrientationNotifications; //是否生成设备转向通知

  51. - (void)beginGeneratingDeviceOrientationNotifications;

  52. - (void)endGeneratingDeviceOrientationNotifications;

  53. @property(nonatomic,getter=isBatteryMonitoringEnabled) BOOL batteryMonitoringEnabled NS_AVAILABLE_IOS(3_0);  // 是否启动电池监控,默认为NO

  54. @property(nonatomic,readonly) UIDeviceBatteryState batteryState NS_AVAILABLE_IOS(3_0);  // 如果禁用电池监控,则电池状态为UIDeviceBatteryStateUnknown

  55. @property(nonatomic,readonly) float batteryLevel NS_AVAILABLE_IOS(3_0);  //电量百分比, 0 .. 1.0。如果电池状态为UIDeviceBatteryStateUnknown,则百分比为-1.0

  56. @property(nonatomic,getter=isProximityMonitoringEnabled) BOOL proximityMonitoringEnabled NS_AVAILABLE_IOS(3_0); // 是否启动接近监控(例如接电话时脸靠近屏幕),默认为NO

  57. @property(nonatomic,readonly)  BOOL proximityState NS_AVAILABLE_IOS(3_0);  // 如果设备不具备接近感应器,则总是返回NO

  58. @property(nonatomic,readonly,getter=isMultitaskingSupported) BOOL multitaskingSupported NS_AVAILABLE_IOS(4_0); // 是否支持多任务

  59. @property(nonatomic,readonly) UIUserInterfaceIdiom userInterfaceIdiom NS_AVAILABLE_IOS(3_2); // 当前用户界面模式

  60. - (void)playInputClick NS_AVAILABLE_IOS(4_2);  // 播放一个输入的声音

  61. @end

  62. @protocol UIInputViewAudioFeedback

  63. @optional

  64. @property (nonatomic, readonly) BOOL enableInputClicksWhenVisible; // 实现该方法,返回YES则自定义的视图能够播放输入的声音

  65. @end

  66. UIKIT_EXTERN NSString *const UIDeviceOrientationDidChangeNotification; // 屏幕方向变化通知

  67. UIKIT_EXTERN NSString *const UIDeviceBatteryStateDidChangeNotification   NS_AVAILABLE_IOS(3_0); // 电池状态变化通知

  68. UIKIT_EXTERN NSString *const UIDeviceBatteryLevelDidChangeNotification   NS_AVAILABLE_IOS(3_0); // 电池电量变化通知

  69. UIKIT_EXTERN NSString *const UIDeviceProximityStateDidChangeNotification NS_AVAILABLE_IOS(3_0); // 接近状态变化通知



重点:这个在iOS8就不能使用了

  
  
  1. #import <Foundation/Foundation.h>
  2. #import <UIKit/UIKitDefines.h>
  3. /* UIAcceleration and UIAccelerometer are deprecated as of iOS 5.0. These classes have been replaced by the Core Motion framework.
  4. */
  5. NS_ASSUME_NONNULL_BEGIN
  6. typedef double UIAccelerationValue;
  7. @protocol UIAccelerometerDelegate;
  8. NS_CLASS_DEPRECATED_IOS(2_0, 5_0, "UIAcceleration has been replaced by the CoreMotion framework") __TVOS_PROHIBITED
  9. @interface UIAcceleration : NSObject
  10. @property(nonatomic,readonly) NSTimeInterval timestamp;    //采样时间
  11. @property(nonatomic,readonly) UIAccelerationValue x;       //偏移量
  12. @property(nonatomic,readonly) UIAccelerationValue y;
  13. @property(nonatomic,readonly) UIAccelerationValue z;
  14. @end
  15. NS_CLASS_DEPRECATED_IOS(2_0, 5_0, "UIAccelerometer has been replaced by the CoreMotion framework") __TVOS_PROHIBITED
  16. @interface UIAccelerometer : NSObject
  17. + (UIAccelerometer *)sharedAccelerometer;
  18. @property(nonatomic) NSTimeInterval updateInterval; //May be capped at a minimum interval
  19. @property(nullable,nonatomic,weak) id<UIAccelerometerDelegate> delegate;
  20. @end
  21. __TVOS_PROHIBITED
  22. @protocol UIAccelerometerDelegate<NSObject>
  23. @optional
  24. - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration NS_DEPRECATED_IOS(2_0, 5_0)  __TVOS_PROHIBITED;
  25. @end
  26. NS_ASSUME_NONNULL_END

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值