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

需求

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

做什么的

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

解决方法

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

问题

在实践中,变通方法不起作用,如果检查器在激活它时被“遮挡”,它就不会记录它的当前状态.

刷新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;
    }
}

最佳答案 您不希望监视翻转事件本身;相反,你想要注意被翻转的状态.

这是一个完整的实现,你只需要调用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;
}
点赞