0
UITapGestureRecognizer *PressRecognizer1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlePress:)];
        [PressRecognizer1 setNumberOfTouchesRequired:1];
         [firstBtn addGestureRecognizer:PressRecognizer1];

-(void)handlePress:(UITapGestureRecognizer*)PressRecognizer {

    NSLog(@"working");
      if (PressRecognizer.state == UIGestureRecognizerStateBegan) {
          UIButton *whichButton=(UIButton *)[PressRecognizer view];
          NSLog(@"whichButton %d\n",whichButton.tag);
          if (whichButton.tag == 0) {
              NSLog(@"currentImageId1 %d",currentImageId1);
              [delegate imageZoom:currentImageId1];
          }

我创建了一个UITabelView自定义单元格,每行有三个图像。当点击一个名为的图像hadlePress方法时。但如果条件,我不会先进入。

4

1 回答 1

1

来自苹果文档:

尽管轻击是离散的手势,但它们对于手势识别器的每个状态都是离散的;因此,相关的动作消息在手势开始时发送,并针对每个中间状态发送,直到(包括)手势的结束状态。因此,处理轻击手势的代码应该测试手势的状态,例如:

- (void)handleTap:(UITapGestureRecognizer *)sender {
     if (sender.state == UIGestureRecognizerStateEnded)     
     {         
          // handling code     
     } 
}

如果您在方法中使用 NSLog(@"%d",sender.state) (在 if 语句之前),您将看到该方法仅在 UIGestureRecognizerStateEnded 状态下被触发,因此您应该更改您的

if (PressRecognizer.state == UIGestureRecognizerStateBegan)

if (PressRecognizer.state == UIGestureRecognizerStateEnded)
于 2013-03-07T11:37:52.340 回答