【问题标题】:UIButton Action Not calling using UITapGestureRecognizer iphone app?UIButton Action 不使用 UITapGestureRecognizer iphone 应用程序调用?
【发布时间】:2012-07-24 13:24:05
【问题描述】:

我正在使用 UITapGestureRecognizer 和 UIButton 在 iPhone 应用程序中工作。我在 self.view 中添加了 UITapGestureRecognizer。当我单击 UIButton 时,它不会调用它的动作,仅调用 Tapgesture 动作。如何解决这个问题?谁能帮忙解决这个问题?

这是我在-ViewDidLoad 中的代码,

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
    [self.view addGestureRecognizer:tapRecognizer];
    [tapRecognizer release];

UIButton *infoButton1 = [UIButton buttonWithType:UIButtonTypeInfoDark];
    infoButton1.frame = CGRectMake(0, 5, 30, 30);
    infoButton1.tag = 1;
    infoButton1.opaque = YES;
    [infoButton1 addTarget:self action:@selector(infoAlert:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
    [toolBarView1 addSubview:infoButton1]; 

-(void) infoAlert:(id) sender
{
    NSLog(@"Get Info Alert"); // Here the control not calling..
}


-(void)handleTapFrom:(UITapGestureRecognizer *)recognizer
{    
    NSLog(@"%@",[NSString stringWithFormat:@"%d", recognizer.view.tag]); // Whenever i touch the button this method only calling.
}

请帮我解决这个问题。提前致谢。

【问题讨论】:

    标签: iphone ios uibutton uitapgesturerecognizer


    【解决方案1】:

    为什么没有人使用堆栈溢出或谷歌的奇妙搜索?

    这是你的答案:https://stackoverflow.com/a/3348532/312312

    在同一个线程中,这似乎是一个更简单的解决方案: https://stackoverflow.com/a/8891030/312312

    【讨论】:

    • 谢谢。我已经在我的代码中尝试了这些项目。但我忘了提到 delgate = self.这就是为什么我得到了答案。感谢您的帮助。
    • 您使用了两种解决方案中的哪一种?第二种方法似乎更简单
    • 我用来解决这个问题的第二个答案。非常感谢。
    • 我使用了搜索并到达了这里。所以我想它可以工作......谢谢你的提示,看起来手势识别器总是有更多的小惊喜。
    【解决方案2】:

    这是最好的解决方案: 在您声明您的 tapRecognizer 后(我指的是您的问题代码)添加以下内容:

      tapRecognizer.cancelsTouchesInView = NO;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-24
      相关资源
      最近更新 更多