如何使用淡入淡出动画设置屏幕亮度? [英] How to set screen brightness with fade animations?

查看:145
本文介绍了如何使用淡入淡出动画设置屏幕亮度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为iOS 5.1+上的屏幕亮度动画设置动画?我正在使用[UIScreen mainScreen] setBrightness:(float)],但我认为突然的更改很丑.

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 is ugly.

推荐答案

我不知道这是否可以通过其他方式动画化",但是您可以自己完成. 例如,以下示例代码已连接到UI中的全亮"和半亮"按钮.它使用 performSelector ... afterDelay 每10毫秒将亮度更改1%,直到达到目标亮度.您将根据一些实验来选择合适的变化率.我认为实际上刷新率是60 hz,因此以小于1/60秒的间隔进行更改可能没有意义(我选择示例率是为了获得很好的数学效果).尽管您可能想在非UI线程上执行此操作,但它不会阻止UI.

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];
    }
}

这篇关于如何使用淡入淡出动画设置屏幕亮度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆