gpt4 book ai didi

ios - 滑动 ios 手势

转载 作者:行者123 更新时间:2023-11-29 03:16:19 29 4
gpt4 key购买 nike

我正在尝试创建一个简单的应用程序,用户可以在其中将手指保持在屏幕上的同时向左然后向右滑动。我想计算他们总共进行了多少次滑动,包括方向的改变。我正在使用带方向的 uiswipegesture,但它只在新滑动时才调用 Action 。更有意义的是,它几乎是在测试用户在特定时间范围内可以从左向右移动手指并再次移动多少次。目前我的 viewdidload 中有这些方法

UISwipeGestureRecognizer *oneFingerSwipeLeft = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:@selector(oneFingerSwipeLeft:)];
[oneFingerSwipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:oneFingerSwipeLeft];

然后这是 Action

-(void) oneFingerSwipeLeft:(UIGestureRecognizer*)recognizer {
NSLog(@"user swipped left");
}

如有任何帮助,我们将不胜感激。

最佳答案

我会做这样的事情。

设置一些变量来存储您正在跟踪的内容:

@property (nonatomic) int swipeCount;
@property (nonatomic) CGPoint previousLocation;

创建一个 UIPanGestureRecognizer:

UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didRecognizePanWithGestureRecognizer:)];
[gesture setDelegate:self];
[self.view addGestureRecognizer:gesture];

处理回调

- (void)didRecognizePanWithGestureRecognizer:(UIPanGestureRecognizer *)gestureRecognizer
{
switch (gestureRecognizer.state)
{
case UIGestureRecognizerStateBegan:
[self handleGestureBeganWithRecognizer:gestureRecognizer];
break;

case UIGestureRecognizerStateChanged:
[self handleGestureChangedWithRecognizer:gestureRecognizer];
break;

case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateCancelled:
case UIGestureRecognizerStateFailed:
[self handleGestureEndedWithRecognizer:gestureRecognizer];
break;

default:
break;
}
}

在用户来回滑动时跟踪您要捕获的信息

- (void)handleGestureBeganWithRecognizer:(UIPanGestureRecognizer *)gestureRecognizer
{
[self setSwipeCount:0];
[self setPreviousTouchLocation:[gestureRecognizer locationInView:self.view]];
}

- (void)handleGestureChangedWithRecognizer:(UIPanGestureRecognizer *)gestureRecognizer
{
CGPoint currentTouchLocation = [gestureRecognizer locationInView:self.view];
CGFloat delta = currentTouchLocation.x - self.previousTouchLocation.x;
[self setPreviousTouchLocation:currentTouchLocation];

//... figure out if they changed directions based on delta positive or negative

}

- (void)handleGestureEndedWithRecognizer:(UIPanGestureRecognizer *)gestureRecognizer
{
//.... finish up
}

关于ios - 滑动 ios 手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21663563/

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