我是iOS应用开发的新手,我试图改变屏幕亮度,但它突然发生了。有没有办法平滑地设置屏幕亮度?
答案 0 :(得分:3)
我猜你可以像这样设置一个重新发生的计时器:
var timer = Timer.scheduledTimer(timeInterval: 0.05, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)
并添加更新方法:
func update() {
UIScreen.main.brightness = UIScreen.main.brightness - CGFloat(0.1)
if UIScreen.main.brightness == CGFloat(0.5) { // or any brightness you want.
timer.invalidate()
}
}
只需调整计时器间隔并降低亮度,直到找到所需内容为止。
答案 1 :(得分:3)
而不是计时器,Swift扩展名:
extension UIScreen {
private static let step: CGFloat = 0.1
static func animateBrightness(to value: CGFloat) {
guard fabs(UIScreen.main.brightness - value) > step else { return }
let delta = UIScreen.main.brightness > value ? -step : step
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
UIScreen.main.brightness += delta
animateBrightness(to: value)
}
}
}