编辑:我在 UIView 中有这个方法,它们位于多个视图控制器的顶部,这些视图控制器是 UIPageViewController 的“数据源”
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setBackgroundColor:[UIColor clearColor]];
UITapGestureRecognizer *tapGR =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
[tapGR setDelegate:self];
[tapGR setNumberOfTapsRequired:1];
[self addGestureRecognizer:tapGR];
UITapGestureRecognizer *doubleTapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTap:)];
[doubleTapGR setNumberOfTapsRequired:2];
[self addGestureRecognizer:doubleTapGR];
[tapGR requireGestureRecognizerToFail :doubleTapGR];
[tapGR release];
[doubleTapGR release];
}
return self;
}
-(void)handleTap:(UITapGestureRecognizer *)tapRecognizer{
if (!(tapRecognizer.state == UIGestureRecognizerStatePossible)) {
[[NSNotificationCenter defaultCenter]postNotification:[NSNotification notificationWithName:kTapOnCenterNotificationName object:nil]];
}
}
-(void)handleDoubleTap:(UITapGestureRecognizer *)doubleTapRecognizer{
CGPoint point = [doubleTapRecognizer locationInView:self];
LSSharedVariables *sharedVariables = [LSSharedVariables sharedInstance];
[sharedVariables setDoubleTapPoint:point];
[[NSNotificationCenter defaultCenter]postNotification:[NSNotification notificationWithName:kLongPressNotificationName object:nil]];
}
即使我指定仅当手势识别器状态等于 UIGestureRecognizerStateEnded 时才应执行操作,日志也会显示多次。我只想在这个块中执行一次操作,我应该怎么做?
编辑:我发现双击方法被调用的次数与 UIPageViewController 的页面一样多。我不明白的是为什么singleTapGestureRecognizer 不一样。