UIPanGesture not working to change brightness on iOS 16.0+

34 Views Asked by At

I am using UIPanGesture to control the system brightness. Till now it was working well on iOS 15 to the previous version. But on iOS 16 the value only changes for one step.

Here is my code


    @objc func panOnView(gesture: UIPanGestureRecognizer)  {
        
        let yTranslation = gesture.translation(in: gesture.view).y
        let xTranslation = gesture.translation(in: gesture.view).x
        
        // The slide distance needed to equal one value in the slider
        // let tolerance: CGFloat = 20
        if abs(yTranslation) >= abs(xTranslation) {
            if (gesture.velocity(in: self.view).y > 0)
            {
                var brightness: Float = Float(UIScreen.main.brightness)
                if(brightness >= 0.01 && brightness <= 1.00) {
                    
                    brightness = brightness - 0.01
                    UIScreen.main.brightness = CGFloat(brightness)
                    
                } 
                //  print("pan on down")
                
            } else if (gesture.velocity(in: self.view).y < 0) {
                var brightness: Float = Float(UIScreen.main.brightness)
                if(brightness <= 0.99 && brightness >= 0.00 ) {
                    
                    brightness = brightness + 0.01
                    UIScreen.main.brightness = CGFloat(brightness)
                    
                } else {
                    
                }
                
                //    print("pan on up")
            }
            gesture.setTranslation(.zero, in: gesture.view)
        }
        
    }
    

1

There are 1 best solutions below

1
Kerem DEMİR On

It doesn't seem to be a problem but you can try this code.

@objc func panOnView(gesture: UIPanGestureRecognizer) {
let yTranslation = gesture.translation(in: gesture.view).y
let xTranslation = gesture.translation(in: gesture.view).x

if abs(yTranslation) >= abs(xTranslation) {
    if gesture.state == .began || gesture.state == .changed {
        let brightnessChange: Float = 0.01
        var brightness = Float(UIScreen.main.brightness)
        
        if gesture.velocity(in: self.view).y > 0 {
            brightness -= brightnessChange
        } else if gesture.velocity(in: self.view).y < 0 {
            brightness += brightnessChange
        }
        
        brightness = max(0.0, min(1.0, brightness))
        UIScreen.main.brightness = CGFloat(brightness)
    }
    
    gesture.setTranslation(.zero, in: gesture.view)
}}