当用户在屏幕上的特定位置点击时,我想调用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来调用分接选择器,但这种方法不允许我找到非常重要的touchPoint。
我很感激任何帮助。非常感谢。
答案 0 :(得分:1)
您可以使用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);
}
答案 1 :(得分:0)
在viewDidLoad
执行此操作:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
选择器方法:
-(void)dismissKeyboard{
[self.view endEditing:YES];
}
使用此方法可以确定点击手势。