简体   繁体   English

关闭屏幕

[英]Turning screen off

I need my application to be running without the iPhone going to sleep. 我需要我的应用程序在没有iPhone进入睡眠的情况下运行。 But I'd like to turn the screen off. 但我想关闭屏幕。 Something similar is done in the Phone application, when you speak on the phone. 当您通过电话讲话时,在电话应用程序中完成类似的操作。

I prevent the iPhone from going to sleep in the following way: [ [UIApplication sharedApplication] setIdleTimerDisabled: YES]; 我通过以下方式防止iPhone进入睡眠状态:[[UIApplication sharedApplication] setIdleTimerDisabled:YES];

But how can I turn the screen off? 但是如何关闭屏幕呢? And how do I turn it back, when user touched the screen? 当用户触摸屏幕时,如何将其关闭?

Thanks. 谢谢。

Update: This method has been deprecated. 更新:此方法已被弃用。 See the comment by Timothée Boucher below. 请参阅TimothéeBoucher的评论。


You can turn the screen off via the proximity sensor, but there is no other public way to put the screen to sleep. 您可以通过接近传感器关闭屏幕,但没有其他公共方法可以让屏幕进入睡眠状态。

-[UIApplication setProximitySensingEnabled:(BOOL)]

Well, you can turn the brightness off completely. 好吧,你可以完全关闭亮度。 It does not lock the screen and the device still displays but no LCD backlight makes it almost impossible to see. 它没有锁定屏幕,设备仍然显示,但没有LCD背光使它几乎不可能看到。

- (void) changeSystemBrightness: (NSString *) switchValue {

if ([[UIScreen mainScreen] respondsToSelector:@selector(setBrightness:)]) {
    if (switchValue) {
        if ([switchValue isEqualToString:@"saveDefault"]) {
            //
            self.userBrightness = [UIScreen mainScreen].brightness;
            //NSLog(@"User Brightness: %1.1f", userBrightness);
        } else if ([switchValue isEqualToString:@"restoreDefault"]) {
            [UIScreen mainScreen].brightness = self.userBrightness;
            //NSLog(@"Restore Brightness: %1.1f", userBrightness);
        } else if ([switchValue isEqualToString:@"min"]) {
            //[UIScreen mainScreen].brightness = 0;
        } else if ([switchValue isEqualToString:@"max"]) {
            [UIScreen mainScreen].brightness = 1;
        } else if ([switchValue isEqualToString:@"mid"]) {
            [UIScreen mainScreen].brightness = 0.5;
        }
    } else {
        [UIScreen mainScreen].brightness = self.userBrightness;
        //NSLog(@"Restore Brightness: %1.1f", userBrightness);
    }
}

} }

First save user's system brightness level 首先保存用户的系统亮度级别

[self changeSystemBrightness:@"saveDefault"];  

After that you can simply turn off the screen: 之后,您只需关闭屏幕:

[self changeSystemBrightness:@"min"];  

Restore brightness: 恢复亮度:

[self changeSystemBrightness:@"restoreDefault"];  

iOS restores default system brightness once the screen is turned off normally (lock/unlock) so you have to detect and handle that. 一旦屏幕正常关闭(锁定/解锁),iOS将恢复默认系统亮度,因此您必须检测并处理它。

I can't confirm that that's a public functionality, but I know that there is a proximity sensor which can sense whether the phone is near your face or not. 我不能确认这是一个公共功能,但我知道有一个接近传感器可以感知手机是否接近你的脸。 Try digging in and figuring out if that sensor is publicly available, and then which function might be turning the screen off. 尝试挖掘并确定该传感器是否公开,然后哪个功能可能会关闭屏幕。

There is no official (public) programmatic way to turn the screen on or off, or even to change the brightness of the display. 没有官方(公共)编程方式来打开或关闭屏幕,甚至改变显示器的亮度。 A few apps "fake" a brightness change by super-imposing a transparent black view on top of your view and changing its opacity to give the appearance of a change in brightness (the back-light will remain on, though, so it will never look like the screen is off and you won't save any battery). 一些应用程序通过在视图顶部施加透明的黑色视图并改变其不透明度以使亮度发生变化的外观来“伪造”亮度变化(背光将保持开启,因此它永远不会看起来屏幕关闭,你不会保存任何电池)。

ya thair is the way you can use undocumented function GSEventSetBacklightFactor(1); ya thair是你可以使用未记录的函数GSEventSetBacklightFactor(1)的方法; this will make screen dim. 这将使屏幕变暗。 if you replace 1 with 0 your screen will be off. 如果将0替换为0,则屏幕将关闭。 then you have to press home button.for using this u have to import a priate framework graphicservice framework 然后你必须按home键。使用这个你必须导入一个priate框架图形服务框架

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM