Ben Ben - 11 days ago 5
Objective-C Question

Can a gesture recogniser control iOS display brightness?

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

Answer

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

Comments