需要
当用户将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;
}
使用加速度计来确定手机是否面朝下以激活接近审查程序效果良好。使用快速刷新率来确定手机何时翻转是至关重要的,就像你在已经被遮挡的情况下激活了接近检测仪一样,屏幕将保持打开状态。一旦手机关机,你可以降低刷新率以节省电池。