1

编辑:我在 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 不一样。

4

2 回答 2

1
- (void) handleDoubleTap : (UIGestureRecognizer*) sender
{
NSLog (@"Double tap is being handled here");
}

- (void) handleSingleTap : (UIGestureRecognizer*) sender
{
NSLog (@"Single tap is being handled here");
}

- (void) loadView
{
UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc] initWithTarget : self action : @selector (handleDoubleTap];
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget : self action : @selector (handleSingleTap];

[singleTap requireGestureRecognizerToFail : doubleTap];

[doubleTap setDelaysTouchesBegan : YES];
[singleTap setDelaysTouchesBegan : YES];

[doubleTap setNumberOfTapsRequired : 2];
[singleTap setNumberOfTapsRequired : 1];

[self.view addGestureRecognizer : doubleTap];
[self.view addGestureRecognizer : singleTap];

[singleTap release];
[doubleTap release];
}

试试这个

于 2012-06-18T16:20:02.243 回答
0

听起来您可能有多个对象在监听触摸,并且在它发生时都独立地调用“handleDoubleTap”。这可以解释为什么当双击事件发生时你会看到它被多次调用。

我将首先搜索您的代码以验证您没有多次添加它,因为您遇到的行为不是触摸事件的标准行为。

于 2012-06-19T15:58:05.987 回答