gpt4 book ai didi

uipangesturerecognizer - 如何确定 UIPanGesture 的方向(向上或向下)?

转载 作者:行者123 更新时间:2023-12-02 05:25:49 36 4
gpt4 key购买 nike

我的目标是使用向上或向下平移手势设置 iPhone 的亮度。如果有一种简单的方法可以确定平底锅的方向,我想不通。我添加了一个滑动手势识别器,然后是以下代码:

- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer           shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer     *)otherGestureRecognizer
{
return YES;
}

但是我的 pan: 方法似乎永远无法确定平移的方向。我已将“self”设置为两个手势识别器的代表。下面的代码是我最近的许多不同尝试。 (向下平移时效果很好——屏幕变暗或变暗,具体取决于向下平移的速度。)

- (IBAction)downSwipe:(UISwipeGestureRecognizer *)recognizer // 1/16/13
{
NSLog (@"downSwipe");
if ((recognizer.state == UIGestureRecognizerStateChanged) ||
(recognizer.state == UIGestureRecognizerStateEnded))
{
NSLog (@"downSwipe in downSwipe");
self.brightness = self.brightness -.1;
NSLog (@"Going Down");
}
}

- (IBAction)upSwipe:(UISwipeGestureRecognizer *)recognizer // 1/16/13
{
NSLog (@"upSwipe");
if ((recognizer.state == UIGestureRecognizerStateChanged) ||
(recognizer.state == UIGestureRecognizerStateEnded))
{
NSLog (@"upSwipe in upSwipe");
self.brightness = self.brightness +.1;
NSLog (@"Going UP");
}
}

- (void)pan:(UIPanGestureRecognizer *)recognizer // 1/12/13
{

if ((recognizer.state == UIGestureRecognizerStateChanged) ||
(recognizer.state == UIGestureRecognizerStateEnded))
{
if (UISwipeGestureRecognizerDirectionDown)
{
NSLog (@"downSwipe in pan");
self.brightness = self.brightness -.005;
NSLog (@"Going DOWN in pan");
}
else if (UISwipeGestureRecognizerDirectionUp)
{
NSLog (@"upSwipe in pan");
self.brightness = self.brightness +.005;
NSLog (@"Going UP in pan");
}
}
}

下面的输出表示连续向上平移大约半英寸。 (平移:误认为是向下的平移。)

2013-01-17 17:49:57.774 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.783 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.794 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.801 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.811 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.818 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.832 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.839 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.848 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.855 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.864 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.871 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.881 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.887 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.896 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.903 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.910 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.919 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.929 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.935 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.945 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.951 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.960 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:57.966 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:57.998 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:58.004 SeriousPanicButton[9426:707] Going DOWN in pan
2013-01-17 17:49:58.022 SeriousPanicButton[9426:707] downSwipe in pan
2013-01-17 17:49:58.030 SeriousPanicButton[9426:707] Going DOWN in pan

Obviously this if statement is not working:

if (UISwipeGestureRecognizerDirectionDown)

我希望我没有让这比实际情况更难。如果能够以某种方式确定平移手势的方向(向上或向下),甚至不必为滑动手势操心,那就更清晰了。

任何帮助将不胜感激。

最佳答案

坚持不懈。我自己找到了答案。这是确定是否向上或向下平移的代码。如果您想知道方向是左还是右,只需更改 if 语句以测试 velocity.x。

- (void)pan:(UIPanGestureRecognizer *)recognizer // 1/18/13
{
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");
}
}
}

关于uipangesturerecognizer - 如何确定 UIPanGesture 的方向(向上或向下)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14389894/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com