How to set screen brightness with fade animations?

后端 未结 7 1538
梦如初夏
梦如初夏 2021-01-02 11:32

Is it possible to animate the screen brightness change on iOS 5.1+? I am using [UIScreen mainScreen] setBrightness:(float)] but I think that the abrupt change i

7条回答
  •  -上瘾入骨i
    2021-01-02 12:24

    I don't know if this is "animatable" in some other way, but you could do it yourself. For instance the following example code was hooked up to "Full Bright" and "Half Bright" buttons in the UI. It uses performSelector...afterDelay to change the brightness by 1% every 10ms till the target brightness is reached. You would pick an appropriate change rate based on some experimenting. Actually the refresh rate is, I think, 60 hz so there is probably no point in doing a change at an interval smaller than 1/60th of a second (My example rate was chosen to have nice math). Although you might want to do this on a non-UI thread, it doesn't block the UI.

    - (IBAction)fullBright:(id)sender {
        CGFloat brightness = [UIScreen mainScreen].brightness;
        if (brightness < 1) {
            [UIScreen mainScreen].brightness += 0.01;
            [self performSelector:@selector(fullBright:) withObject:nil afterDelay:.01];
        }
    }
    
    - (IBAction)halfBright:(id)sender {
        CGFloat brightness = [UIScreen mainScreen].brightness;
        if (brightness > 0.5) {
            [UIScreen mainScreen].brightness -= 0.01;
            [self performSelector:@selector(halfBright:) withObject:nil afterDelay:.01];
        }
    }
    

提交回复
热议问题