1

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
    }
}
TylerP
  • 9,600
  • 4
  • 39
  • 43
KaasCoder
  • 251
  • 5
  • 14
  • I don't believe your code was _ever_ right. There are three problems. (1) Keeping a `previousLocationY` is not how you do a pan gesture recognizer. (2) Continually comparing velocities is not how you do vertical-specific or horizontal-specific pan gesture recognizers. (3) Most important of all, looking at the change in point distance of the gesture and just using it directly is not at all how to tell the screen brightness how much to change! You have to map between the domain of your limits to the domain of the screen brightness limits. – matt Oct 29 '22 at 19:30
  • (1) Keeping the previous location is also used in Apple example, (2) How would you suggest I determine if a Pan gesture is moving up and between 315º and 45º, (3) the question was how I can change the screen brightness in 0.01 steps instead of 0.025 steps (works in iPadOS 15 not in iPadOS 16) Is this maybe because of the always on screen in iPhone 14? – KaasCoder Nov 08 '22 at 20:40
  • Well clearly the first thing to decide is whether the issue is iOS 16 or the iPhone 14. Which is it? – matt Nov 08 '22 at 21:42
  • This problem showed up starting with iOS 16. I can document it was not there in iOS 14. There are enough clues here though for a "fix" of the behavior. My observation is if the delta step on brightness was too small, the value never changes. I can't explain why this is the case though. No clues in the documentation I looked at. – truedat101 Aug 24 '23 at 03:34

0 Answers0