gpt4 book ai didi

ios - 调用 touchesEnded : when tapped but not swiped

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:18:32 25 4
gpt4 key购买 nike

我想在用户点击屏幕上的特定位置而不是在用户在屏幕上滑动时调用 touchesEnded: 方法。我怎样才能做到这一点?

从调试来看,touchesEnded: 似乎在用户点击或滑动时被调用。我想我可以添加一个局部变量来跟踪用户是否像下面那样滑动,但我认为有更强大的方法来处理这个问题:

BOOL trackSwipe  = NO;  // Local variable

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
trackSwipe = YES;
// do something
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
if (trackSwipe == YES)
return;
else
{
CGPoint touchPoint = [[touches anyObject] locationInView:self];
// do additional work
}
}

我也研究过添加 UITapGestureRecognizer 来调用点击选择器,但这种方法不允许我找到非常重要的触摸点。

我很感激这方面的任何帮助。非常感谢。

最佳答案

您可以尝试使用 UITapGestureRecognizer

ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIGestureRecognizerDelegate>

@end

ViewController.m:

- (void)viewDidLoad
{
[super viewDidLoad];

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapMethod:)];
recognizer.numberOfTapsRequired = 1;
recognizer.numberOfTouchesRequired = 1;
recognizer.delegate = self;

[self.view addGestureRecognizer:recognizer];
}

- (void)tapMethod:(UITapGestureRecognizer *)recognizer
{
CGPoint touch = [recognizer locationInView:yourViewHere];
NSLog(@"X: %f Y: %f",touch.x,touch.y);
}

关于ios - 调用 touchesEnded : when tapped but not swiped,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22624871/

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