双击问题

时间:2011-09-15 15:04:53

标签: objective-c cocoa-touch

我正在开发具有缩放功能的应用。 在这个应用程序中我有这个按钮。我希望它以多种方式回应攻击:

单击:略微放大。

双击:放大到最大值。

我已经尝试了几种方法来实现这一目标,但没有一种是我想要的。

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tapGesture];

[zoomin addTarget:self action:@selector(zoominMax) forControlEvents:UIControlEventTouchDownRepeat];

两者都可以单击和双击,但是当我按下按钮一次稍微变焦时,几秒钟之后再次按下它不会轻微放大,它会放大到最大值。

可以通过计时器和位置检查来解决这个问题,这样当您点击并再次点击时,您可以确定该位置位于类似区域,并且点击发生在计时器范围内。

但这是我真正需要的吗? 有更简单的解决方案吗?

2 个答案:

答案 0 :(得分:5)

omz提供的解决方案并不好。

你可以通过简单地添加像这里发布的这些代码行来做到这一点。 Double-tap or two single-taps?

注意神奇的线条: [tapRecg requireGestureRecognizerToFail:doubleTapRecg];

<强> 摘要:

UITapGestureRecognizer *doubleTapRecg = [[UITapGestureRecognizer alloc]
                                             initWithTarget:self 
                                             action:@selector(doubleTapped:)];
    doubleTapRecg.delegate = self;
    doubleTapRecg.numberOfTapsRequired = 2;
    doubleTapRecg.numberOfTouchesRequired = 1;
    [view addGestureRecognizer:doubleTapRecg];


    UITapGestureRecognizer *tapRecg = [[UITapGestureRecognizer alloc]
                                       initWithTarget:self 
                                       action:@selector(tapped:)];
    tapRecg.delegate = self;
    tapRecg.numberOfTapsRequired = 1;
    tapRecg.numberOfTouchesRequired = 1;
    [view addGestureRecognizer:tapRecg];
    [tapRecg requireGestureRecognizerToFail:doubleTapRecg];


    [doubleTapRecg release];
    [tapRecg release];

答案 1 :(得分:4)

你可以用两个手势识别器和一个计时器来做到这一点:

UITapGestureRecognizer *tapGestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)] autorelease];
[myView addGestureRecognizer:tapGestureRecognizer];

UITapGestureRecognizer *doubleTapGestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)] autorelease];
doubleTapGestureRecognizer.numberOfTapsRequired = 2;
[myView addGestureRecognizer:doubleTapGestureRecognizer];

在稍微放大之前,您必须稍微延迟tap:操作,因为第一次点按后可能会有第二次点按:

- (void)tap:(UITapGestureRecognizer *)recognizer
{
    [self performSelector:@selector(singleTap) withObject:nil afterDelay:0.25];
}

- (void)singleTap
{
    //slightly zoom in...
}

- (void)doubleTap:(UITapGestureRecognizer *)recognizer
{
    //Cancel the timer for the single tap action:
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap) object:nil];
    //zoom in to the max zoom level...
}