-->

Can a gesture recogniser control iOS display brigh

2019-08-27 22:05发布

问题:

I'd like to let the user control the brightness in an iOS app using a gesture recogniser to swipe up and down. Can gestures even control brightness? I've done it before using a slider and the brightness property of UIScreen...

[UIScreen mainScreen].brightness = 0.5;

I want to be able to control the brightness as you would with a slider (like the iBooks app) but instead of a slider use a gesture recogniser. The idea is that the gesture continuously changes the brightness value as long as a touch is being recognised and the finger moves either up or down. I wasn't sure if I should use a pan gesture and limit the direction somehow to up or down, or use a swipe gesture (but obviously that detects one swipe and doesn't continuously update a value).

Perhaps moving a finger up a specified amount on the screen increments the 0.0 - 1.0 range of the brightness value. I'm not sure how to approach it so i'm just thinking out loud.

Any ideas? Thanks a lot.

EDIT: Ok so I found this code on another similar question

- (void)pan:(UIPanGestureRecognizer *)recognizer
{
    if ((recognizer.state == UIGestureRecognizerStateChanged) ||
        (recognizer.state == UIGestureRecognizerStateEnded))
    {
        CGPoint velocity = [recognizer velocityInView:self.view];

        if (velocity.y >0)   // panning down
        {
            self.brightness = self.brightness -.02;
                 NSLog (@"Decreasing brigntness in pan");
        }
        else                // panning up
        {
            self.brightness = self.brightness +.02;
              NSLog (@"Increasing brigntness in pan");
        }
    }
}

It seems to at least print an NSLog every time you gesture up or down, I guess I now just want to know if there's a way to use NSLog to find out the current brightness value rather than it just telling you if it detected a gesture (unless i'm being stupid and the NSLog i'm using already tells me that?). Thanks, this would be really useful to know because I can't run on the device. :-D

回答1:

Yes! Just use a UIPanGestureRecognizer and adjust the brightness value accordingly as the pan gesture is called. Check the Apple docs.



回答2:

To Increase and decrease the brightness of the Screen

For Swift

1)How to Add PanGesture in View

let panGesture = UIPanGestureRecognizer.init(target: self, action: #selector(pan(recognizer:)))
panGesture.delegate = self
panGesture.minimumNumberOfTouches = 1
mp.view.addGestureRecognizer(panGesture)

2)Code to increase and Decrease the Brightness of Screen using PanGesture

func pan(recognizer:UIPanGestureRecognizer){
    if recognizer.state == UIGestureRecognizerState.changed || recognizer.state == UIGestureRecognizerState.ended {
        let velocity:CGPoint = recognizer.velocity(in: self.view)

        if velocity.y > 0{
            var brightness: Float = Float(UIScreen.main.brightness)
            brightness = brightness - 0.03
            UIScreen.main.brightness = CGFloat(brightness)
            print("Decrease brigntness in pan")
        }
        else {
            var brightness: Float = Float(UIScreen.main.brightness)
            brightness = brightness + 0.03
            UIScreen.main.brightness = CGFloat(brightness)
            print("Increase brigntness in pan")
        }
    }
}