如何使用淡入淡出animation设置屏幕亮度?

iOS 5.1+上的屏幕亮度变化可以animation吗? 我正在使用[UIScreen mainScreen] setBrightness:(float)]但我认为突然变化是丑陋的。

我不知道这是否以其他方式是“可动的”,但你可以自己动手。 例如,以下示例代码被连接到UI中的“全亮”和“半亮”button。 它使用performSelector … afterDelay每10ms将亮度改变1%,直到达到目标亮度。 你会根据一些实验select一个合适的变化率。 其实刷新率是,我认为,60赫兹,所以可能没有意义在一个小于1/60秒的时间间隔(我的例子select有好的math)进行改变。 尽pipe您可能想要在非UI线程上执行此操作,但不会阻止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]; } } 

当尝试使用之前的animation进行animation处理时,我遇到了接受答案的问题。 此解决scheme取消正在进行的animation并为新值创buildanimation:

 extension UIScreen { func setBrightness(_ value: CGFloat, animated: Bool) { if animated { _brightnessQueue.cancelAllOperations() let step: CGFloat = 0.04 * ((value > brightness) ? 1 : -1) _brightnessQueue.add(operations: stride(from: brightness, through: value, by: step).map({ [weak self] value -> Operation in let blockOperation = BlockOperation() unowned let _unownedOperation = blockOperation blockOperation.addExecutionBlock({ if !_unownedOperation.isCancelled { Thread.sleep(forTimeInterval: 1 / 60.0) self?.brightness = value } }) return blockOperation })) } else { brightness = value } } } private let _brightnessQueue: OperationQueue = { let queue = OperationQueue() queue.maxConcurrentOperationCount = 1 return queue }() 

根据查理·普莱斯(Charlie Price)的精彩回答,这里有一个方法可以将屏幕亮度的变化“animation化”为任何想要的值。

 - (void)graduallyAdjustBrightnessToValue:(CGFloat)endValue { CGFloat startValue = [[UIScreen mainScreen] brightness]; CGFloat fadeInterval = 0.01; double delayInSeconds = 0.01; if (endValue < startValue) fadeInterval = -fadeInterval; CGFloat brightness = startValue; while (fabsf(brightness-endValue)>0) { brightness += fadeInterval; if (fabsf(brightness-endValue) < fabsf(fadeInterval)) brightness = endValue; dispatch_time_t dispatchTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); dispatch_after(dispatchTime, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [[UIScreen mainScreen] setBrightness:brightness]; }); } } 

或者你可以使用NSTimer代替while循环和performSelector。

finalValue – 是你想要达到的价值。

定时器每次闪光30次,持续时间为0.02秒(您可以select不同但平滑的东西)并更改亮度值。

 weak var timer: NSTimer? var count = 1 let maxCount = 30 let interval = 0.02 timer = NSTimer .scheduledTimerWithTimeInterval(interval, target: self, selector: #selector(changeBrightness), userInfo: nil, repeats: true) func changeBrightness() { guard count < maxCount else { return } let currentValue = UIScreen.mainScreen().brightness let restCount = maxCount - count let diff = (finalValue - currentValue) / CGFloat(restCount) let newValue = currentValue + diff UIScreen.mainScreen().brightness = newValue count += 1 }