问题描述
我想让用户使用手势识别器上下滑动来控制 iOS 应用中的亮度.手势甚至可以控制亮度?我在使用滑块和 UIScreen 的亮度属性之前已经完成了...
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;
我希望能够像使用滑块(如 iBooks 应用程序)一样控制亮度,但使用手势识别器代替滑块.这个想法是只要触摸被识别并且手指向上或向下移动,手势就会不断改变亮度值.我不确定我是否应该使用平移手势并以某种方式将方向限制为向上或向下,或者使用滑动手势(但显然这会检测到一次滑动并且不会持续更新值).
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).
也许将手指在屏幕上向上移动指定量会增加 0.0 - 1.0 范围的亮度值.我不知道如何处理它,所以我只是在大声思考.
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.
好的,所以我在另一个类似的问题上找到了这段代码
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");
}
}
}
每次向上或向下手势时似乎至少会打印一个 NSLog,我想我现在只想知道是否有办法使用 NSLog 来找出当前亮度值 而不是它只是告诉你它是否检测到了一个手势(除非我很愚蠢并且我正在使用的 NSLog 已经告诉我了?).谢谢,知道这真的很有用,因为我无法在设备上运行.:-D
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
推荐答案
Yes! 只需使用 UIPanGestureRecognizer
并相应地调整 brightness
值当平移手势被调用时.查看 Apple 文档.
Yes! Just use a UIPanGestureRecognizer
and adjust the brightness
value accordingly as the pan gesture is called. Check the Apple docs.
这篇关于手势识别器可以控制 iOS 显示亮度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!