应用处于resignActive状态时无法设置亮度

时间:2019-04-22 08:01:55

标签: ios brightness

当应用程序进入背景时,我正在将屏幕的亮度降低到其原始值,更具体地说,我在applicationWillResignActive中使用了此方法,但由于某种原因代码无法正常工作,我在哪里错过了?

亮度增加代码与减少代码相同,但是后者不起作用........

Objective-C
//increase light
-(void)graduallyHighlightScreen{
    CGFloat currentBrightness = [UIScreen mainScreen].brightness;
    if (currentBrightness >= ScanBrightness){
        return;
    }else{
        if (!_highlighted) {
            _originalBrightness = currentBrightness;
            _highlighted = YES;
            [self createLightChangeTimer];
        }else{
            return;
        }
    }
}

//decrease light(I call this in applicationWillResignActive)
-(void)graduallyDarkenScreen
{
    if (_highlighted) {
        [self createLightChangeTimer];
        _highlighted = NO;
    }else{
        return;
    }
}

//create light change timer
-(void)createLightChangeTimer
{
    self.endCount = 0;
    [self.lightChangeTimer invalidate];
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.01
                                                      target:self
                                                    selector:@selector(changeLight)
                                                    userInfo:nil
                                                     repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
    timer.fireDate = [NSDate date];
    self.lightChangeTimer = timer;
}

-(void)changeLight
{
    CGFloat increment = (ScanBrightness - _originalBrightness) / BrightnessChangeCount;

    //stop increasing 
    if (self.highlighted && (fabs([UIScreen mainScreen].brightness - ScanBrightness) < 0.001 ||  self.endCount == BrightnessChangeCount))
    {
        [self.lightChangeTimer invalidate];
    }
    //stop decreasing
    else if (!self.highlighted && (fabs([UIScreen mainScreen].brightness - _originalBrightness) < 0.001 ||  self.endCount == BrightnessChangeCount))
    {
        [self.lightChangeTimer invalidate];
    }
    //increasing brightness
    else if(self.highlighted)
    {
        [UIScreen mainScreen].brightness += increment;
        self.endCount ++;
    }
    //decreasing brightness(this won't work when app is in background)
    else if (!self.highlighted)
    {
        [UIScreen mainScreen].brightness -= increment;
        self.endCount ++;
    }
}

我希望亮度会降低到其原始值,但是当应用程序进入背景时,触发了降低代码,亮度只是稍有变化。

0 个答案:

没有答案