在遮挡时激活接近监控以关闭iphone屏幕

时间:2012-03-16 01:49:02

标签: iphone ios proximity

需要

当用户将iphone倒置在q桌面上时,我想关闭屏幕。同时我不希望一直启用接近监视器,因为这对用户来说非常不舒服,并且根据您如何抓取设备而错过很多次。

的内容

这是在晚上离开并在运行应用程序时节省电池和屏幕寿命。

解决方法

我想的是使用加速度计来确定面部是否已关闭,如果是,则激活接近检查器。简单的东西......

问题

在实践中,解决方法不起作用,如果检查员被遮挡了,那就接缝了#34;当你激活它时,它不会注册它的当前状态。

刷新UIDevice的一些方法?

我使用

-(id)init {
    if ((self = [super init])) 
    {
        NSLog(@"Init ShakerAnalizer");
        accelerometer = [UIAccelerometer sharedAccelerometer];
        accelerometer.delegate = self;
        accelerometer.updateInterval = 5.0f;
    }
    return self;
}

-(void)accelerometer:(UIAccelerometer *)accel didAccelerate:(UIAcceleration *)acceleration
{
    if (accelerometer) 
    {        
        NSLog(@"Accelerometer Z:::  %f", acceleration.z);

        if (acceleration.z > kFlippedThreshold) 
            device.proximityMonitoringEnabled = YES;
        else
            device.proximityMonitoringEnabled = NO;
    }
}

2 个答案:

答案 0 :(得分:2)

您不想监视翻转事件本身;相反,你想要注意被翻转的状态

这是一个完整的实现,您只需在需要时调用monitorForFaceDownOnSurfaceStatus:(可能一直,也许不是),并填写setFaceDownOnSurface:来处理该状态(可能将屏幕亮度设置为最小值)在我的例子中):

- (BOOL)canEnableProximityMonitoring
{
    UIDevice *device = [UIDevice currentDevice];

    BOOL wasEnabled = device.proximityMonitoringEnabled;
    BOOL could;
    device.proximityMonitoringEnabled = YES;
    could = device.proximityMonitoringEnabled;
    device.proximityMonitoringEnabled = wasEnabled;

    return could;
}

BOOL isMonitoringForFaceDown = NO;
- (void)monitorForFaceDownOnSurfaceStatus:(BOOL)shouldMonitor
{
    if ( ![self canEnableProximityMonitoring] ) {
        return;
    }

    UIDevice *device = [UIDevice currentDevice];
    if ( shouldMonitor ) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
        [device beginGeneratingDeviceOrientationNotifications];
    } else {
        [device endGeneratingDeviceOrientationNotifications];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
    }

    if ( isMonitoringForFaceDown != shouldMonitor ) {
        isMonitoringForFaceDown = shouldMonitor;
        [self deviceOrientationChanged:nil];
    }
}

UIDeviceOrientation oldOrientation = UIDeviceOrientationUnknown;
- (void)deviceOrientationChanged:(NSNotification *)note
{
    if ( !note ) {
        [self monitorProximityState:NO];
        return;
    }

    UIDevice *device = [UIDevice currentDevice];
    UIDeviceOrientation newOrientation = device.orientation;
    if ( newOrientation != oldOrientation ) {
        oldOrientation = newOrientation;
        [self monitorProximityState:(oldOrientation == UIDeviceOrientationFaceDown)];
    }
}

BOOL isMonitoringProximity = NO;
- (void)monitorProximityState:(BOOL)shouldMonitor
{   
    UIDevice *device = [UIDevice currentDevice];
    if ( shouldMonitor ) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateChanged:) name:UIDeviceProximityStateDidChangeNotification object:nil];
        device.proximityMonitoringEnabled = YES;
    } else {
        device.proximityMonitoringEnabled = NO;
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceProximityStateDidChangeNotification object:nil];
    }

    if ( isMonitoringProximity != shouldMonitor ) {
        isMonitoringProximity = shouldMonitor;
        [self proximityStateChanged:nil];
    }
}

BOOL oldProximityState = NO;
- (void)proximityStateChanged:(NSNotification *)note
{
    if ( !note ) {
        [self setFaceDownOnSurface:NO];
        return;
    }

    UIDevice *device = [UIDevice currentDevice];
    BOOL newProximityState = device.proximityState;
    if ( newProximityState != oldProximityState ) {
        oldProximityState = newProximityState;
        [self setFaceDownOnSurface:newProximityState];
    }
}

float oldBrightness;
- (void)setFaceDownOnSurface:(BOOL)isFaceDownOnSurface
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        oldBrightness = [UIScreen mainScreen].brightness;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(brightnessChanged:) name:UIScreenBrightnessDidChangeNotification object:[UIScreen mainScreen]];
    });

    float newBrightness = 0;
    if ( isFaceDownOnSurface ) {
        oldBrightness = [UIScreen mainScreen].brightness;
    } else {
        newBrightness = oldBrightness;
    }

    [UIApplication sharedApplication].idleTimerDisabled = isFaceDownOnSurface;
    [UIScreen mainScreen].wantsSoftwareDimming = isFaceDownOnSurface;
    [UIScreen mainScreen].brightness = newBrightness;
}

- (void)brightnessChanged:(NSNotification *)note
{
    oldBrightness = [UIScreen mainScreen].brightness;
}

答案 1 :(得分:0)

使用加速计确定手机是否朝下以激活接近检查器效果很好。使用快速刷新率来确定手机翻转时是否正常,就像您已经遮挡时激活接近检查器一样,屏幕将保持打开状态。一旦手机停止运转,您可以降低刷新率以节省电量。