0

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

Ben
  • 65
  • 2
  • 6
  • Of course it could! Look into UIPanGestureRecognizer examples. – CodaFi Jan 19 '13 at 00:01
  • @ CodaFi Its his first post why not try being more helpful. Stack Overflow is here to help people with coding, if everyone knew exactly what to do this place wouldn't exist – JSA986 Jan 19 '13 at 02:05
  • If you are having trouble knowing whether you can find out what a value is through logging it with NSLog, you should read up on [format strings](http://en.wikipedia.org/wiki/Printf_format_string#Format_placeholders) and how to use functions like `NSLog` and `printf`. I'll give you this one though, `NSLog(@"Brightness = %f",[UIScreen mainScreen].brightness)` will log the brightness, this is a format string and the %f says plug in a `float` value. – Carl Veazey Jan 19 '13 at 12:12

2 Answers2

5

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")
        }
    }
}
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
1

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

Undo
  • 25,519
  • 37
  • 106
  • 129
  • Thanks a lot but have you got any code examples? I've looked around, seen lots of similar unanswered questions on this, but I just can't see how to link a pan gesture with the brightness value. Also is there a way to use `NSLog` to find out the current brightness value? I'm new to programming so I appreciate the help :) – Ben Jan 19 '13 at 10:53
  • @Ben You'd use `NSLog(@"Brightness = %f",[UIScreen mainScreen].brightness)` – Undo Jan 19 '13 at 18:19