有没有办法监控用户的iphone移动,如“提高说话”特征? [英] Is there any way to monitor user's movement of iphone like the "raise to speak" feature?

查看:140
本文介绍了有没有办法监控用户的iphone移动,如“提高说话”特征?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户将iphone抬到脸上时收到通知。就像siri一样。是否有可能?

I want to get notified when the user raises the iphone to his face. Just like siri does. Is it possible?

添加更具体的要求:
当用户将手机放在耳边时,我想让屏幕变暗。我知道可以启用接近传感器来实现这一点。但令人讨厌的是,当用户将手指移到传感器上时,屏幕会不时变暗。所以我想知道如何避免这种情况,只有当用户抬起iphone发言时才会使屏幕变暗?

Add more specific requirement: I want to darken the screen when user put the phone near his ear. I know the Proximity Sensor can be enable to implement this. But it's annoying that the screen will be darkened from time to time when user moves the finger upon the sensor. So I wonder how to avoid this case and only darken the screen when user raise the iphone to speak?

推荐答案

参见使用接近传感器< UIDevice类参考中的/ a>。所以,你:


  • 启用它:

  • Enable it:

UIDevice *device = [UIDevice currentDevice];
device.proximityMonitoringEnabled = YES;


  • 检查是否成功启用;观察如果成功, UIDeviceProximityStateDidChangeNotification 通知;如果没有,您的设备可能无法使用:

  • Check if successfully enabled; observe the UIDeviceProximityStateDidChangeNotification notification if successful; if not, your device may not be capable:

    if (device.proximityMonitoringEnabled)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(handleProximityChange:)
                                                     name:UIDeviceProximityStateDidChangeNotification
                                                   object:nil];
    }
    else
    {
         // device not capable
    }
    


  • 并编写你的选择器:

  • And write your selector:

    - (void)handleProximityChange:(NSNotification *)notification
    {
        NSLog(@"%s proximityState=%d", __FUNCTION__, [[UIDevice currentDevice] proximityState]);
    }
    


  • 为了检测用户是否将它拿到脸上,我可能会将接近传感器与 CMMotionManager 结合起来并查看 gravity 属性,看看他们是否几乎垂直拿着手机。因此,定义一些类属性:

    To detect whether the user is holding it up to their face, I might marry the proximity sensor with the CMMotionManager and look at the gravity property to see if they're holding the phone nearly vertically. So, define a few class properties:

    @property (nonatomic, strong) CMMotionManager *motionManager;
    @property (nonatomic, strong) NSOperationQueue *deviceQueue;
    

    然后你可以启动 CMMotionManager ,寻找设备是否处于垂直位置:

    And then you can start the CMMotionManager, looking for whether the device is held in a vertical position:

    self.deviceQueue = [[NSOperationQueue alloc] init];
    self.motionManager = [[CMMotionManager alloc] init];
    self.motionManager.deviceMotionUpdateInterval = 5.0 / 60.0;
    
    UIDevice *device = [UIDevice currentDevice];
    
    [self.motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryZVertical
                                                            toQueue:self.deviceQueue
                                                        withHandler:^(CMDeviceMotion *motion, NSError *error)
    {
        BOOL vertical = (motion.gravity.z > -0.4 && motion.gravity.z < 0.4 & motion.gravity.y < -0.7);
        if ((vertical && !device.proximityMonitoringEnabled) || (!vertical && device.proximityMonitoringEnabled))
        {
            device.proximityMonitoringEnabled = vertical;
        }
    }];
    

    这些 gravity 阈值是否有意义是很少主观。您也可以,而不是仅仅查看手机是否大致垂直握持,查看其他加速度计数据(例如,他们是否提升了对象)。似乎有很多方法可以给猫皮肤。

    Whether these gravity thresholds make sense is a little subjective. You could also, rather than just looking to see if the phone is being held roughly vertically, look at other accelerometer data (e.g. did they raise the object or not). Seems like there are lots of ways to skin the cat.

    这篇关于有没有办法监控用户的iphone移动,如“提高说话”特征?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆