Since updating to iPadOS 16 I cannot the change screen brightness continually.
The brightness change does not show on the screen or the change starts only when I use the Pan gesture "aggressively". The following code works fine in iPadOS 15 (I can change the brightness in very small steps), but works only intermittently in iPadOS 16.
The print command shows a continuous update of 'Brightness Change' so the pan gesture works fine. But the screen does not react.
UIScreen.main.brightness
also does not work properly in iPadOS 16 (deprecated), changed to: (UIApplication.shared.connectedScenes.first as! UIWindowScene).screen.brightness
private var screen: UIScreen {return (UIApplication.shared.connectedScenes.first as! UIWindowScene).screen}
private var previousLocationY: CGFloat = 0.0
private var direction: Bool = true //going down (darker)
@IBAction func handlePanGesture(_ sender: UIPanGestureRecognizer) {
switch sender.state {
case .began:
self.previousLocationY = sender.location(in: self.view).y
break
case .changed:
self.direction = sender.location(in: self.view).y > self.previousLocationY // down = darker
if abs(sender.velocity(in: self.view).x) < abs(sender.velocity(in: self.view).y) { // Vertical Pan
let change = (sender.location(in: self.view).y - self.previousLocationY) * 0.002
print("***** Brightness Change: \(change), Direction: \(self.direction) *****")
if self.screen.brightness > 0.01 {
self.screen.brightness -= change
}
self.previousLocationY = sender.location(in: self.view).y
}
break
case .ended:
break
default:
break
}
}
Update:
I found sort of a solution: change brightness in 0.025 steps.
It works but the screen flickers because of the stepping. Smaller steps do not work in my case. Not a really good solution.
New Code:
private var change: CGFloat = 0.0
@IBAction func handlePanGesture(_ sender: UIPanGestureRecognizer) {
switch sender.state {
case .began:
self.previousLocationY = sender.location(in: self.view).y
break
case .changed:
self.direction = sender.location(in: self.view).y > self.previousLocationY // down = darker
if abs(sender.velocity(in: self.view).x) < abs(sender.velocity(in: self.view).y) { // Vertical Pan
let currentChange = (sender.location(in: self.view).y - self.previousLocationY) * 0.002//0.002
change += currentChange
print("***** Brightness Change: \(change), Direction: \(self.direction) *****")
if abs(self.change) >= 0.025 { // new: steps
if direction == true { // -> Going Down (Darker)
if self.screen.brightness > 0.01 {
self.screen.brightness -= change
}
} else { // -> Going Up (Lighter)
self.screen.brightness -= change
}
self.change = 0.0
}
self.previousLocationY = sender.location(in: self.view).y
}
break
case .ended:
break
default:
break
}
}