gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 05:57:41 26 4
gpt4 key购买 nike

需要

当用户将他的 iPhone 倒置放在 q table 上时,我想关闭屏幕。同时,我不希望一直启用近距离监视器,因为这对用户来说非常不舒服,并且根据您抓取设备的方式会错过很多次。

有什么用

这是为了在晚上离开并在运行应用程序的同时节省电池和屏幕生命周期。

解决方法

我正在考虑的是使用加速度计来确定面部是否朝下,如果是则激活接近传感器。简单的东西...

问题

在实践中,变通方法不起作用,它接缝是如果检查器在您激活它时被“遮挡”,它不会记录其当前状态。

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

关于iphone - 在被遮挡时激活接近监控以关闭 iPhone 屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9730765/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com