根据pm总结,手机特性包括:
1. 麦克风
2. 摄像头
3. 近场通信(NFC)
4. 蓝牙
5. 多点触屏
6. GPS
7. 二维码
8. 重力感应传感器
9. 线性加速度传感器
10. 温度传感器
11. 距离传感器
12. 陀螺仪传感器
13. 旋转矢量传感器
14. 压力传感器
15. 电子罗盘传感器
16. 光线传感器
17. 方向传感器
第一步,对于ios支持的进行分类。
较常用的传感器:
麦克风,摄像头,蓝牙,多点触控,GPS,二维码
重力感应传感器,线性加速传感器
1) 麦克风,吹的实现
SCListener是一个开源的麦克风检测库,使用该库可以十分容易实现吹的功能的获取,下面对于该库进行简要的分析。
2)多点触控,手势
两个级别的实现,一个是js级别,还有一个是native级别。
js是webview上的,
3)线性加速传感器的“摇”
ios对于摇动进行了封装,直接可以使用MotionEvent来实现,代码如文档中实例,
首先设置view为firstResponder
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)viewDidAppear:(BOOL)animated {
[self becomeFirstResponder];
}然后实现父类中的下列方法:
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"shake");
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
}在motionEnded中实现shake需要触发的事件,如果要区分用户的横向和竖向的shake,可以在motionBegan中记录
acceleration axis的值,与End中的值进行对比来实现判断,在这些方法的实现中,如果处理完了就不用向上传递,如果一直向上传递,在window中如果UIApplication的属性 applicationSupportsShakeToEdit为Yes的话,系统会出现edit的对话框,这就是有的文章说的调用shake的另一种方法。
4)线性加速传感器的倾斜检测
使用Accelerometer需要对应用程序的Info.plist中添加UIRequiredDeviceCapabilities
文档中描述:accelerometer (for accelerometer events)
UIEvent
objects.
应用程序都有单例的Accelerometer,首先是配置Accelerometer
#define kAccelerometerFrequency 50.0 //Hz
-(void)configureAccelerometer
{
UIAccelerometer* theAccelerometer = [UIAccelerometer sharedAccelerometer];
theAccelerometer.updateInterval = 1 / kAccelerometerFrequency;
theAccelerometer.delegate = self;
// Delegate events begin immediately.
}得到
UIAccelerometer的单例,
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
同时,你需要设置它的delegate,以及检测的周期
在代理中实现委托方法:(在ios5中已经deprecated)
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
UIAccelerationValue x, y, z;
x = acceleration.x;
y = acceleration.y;
z = acceleration.z;
// Do something with the values.
}
UIAcceleration是表示加速度类。包含了来自加速计UIAccelerometer的真是数据。它有3个属性的值x、y、z。iphone的加速计支持最高以每秒100次的频率进行轮询。此时是50次。
在不使用加速测量时,记得将其delegate设置为nil
另外对于加速度的存储,文档中列出了两种情形,
一种是检测持续的加速度,就是去除掉加速中的重力因素,实例代码如下:
#define kFilteringFactor 0.1
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
// Use a basic low-pass filter to keep only the gravity component of each axis.
accelX = (acceleration.x * kFilteringFactor) + (accelX * (1.0 - kFilteringFactor));
accelY = (acceleration.y * kFilteringFactor) + (accelY * (1.0 - kFilteringFactor));
accelZ = (acceleration.z * kFilteringFactor) + (accelZ * (1.0 - kFilteringFactor));
// Use the acceleration data.
}还有一种是检测发生的单次的加速度,这个时候就要去除掉持续的加速的因素,实例代码如下:
#define kFilteringFactor 0.1
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
// Subtract the low-pass value from the current value to get a simplified high-pass filter
accelX = acceleration.x - ( (acceleration.x * kFilteringFactor) + (accelX * (1.0 - kFilteringFactor)) );
accelY = acceleration.y - ( (acceleration.y * kFilteringFactor) + (accelY * (1.0 - kFilteringFactor)) );
accelZ = acceleration.z - ( (acceleration.z * kFilteringFactor) + (accelZ * (1.0 - kFilteringFactor)) );
// Use the acceleration data.
}一般游戏中使用的持续的加速度,使用第一种的比较常见。可以使用第二种来进行摇的检测实现。
本文总结了iOS设备的各种传感器,包括麦克风、摄像头、NFC、蓝牙、多点触屏、GPS等,并重点介绍了如何利用麦克风实现吹气功能、多点触控的手势识别、线性加速传感器在摇动和倾斜检测中的应用。通过代码示例展示了如何使用iOS SDK中的API进行传感器操作。
1470

被折叠的 条评论
为什么被折叠?



