使用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.
}
请帮我解决这个问题。提前谢谢。
答案 0 :(得分:3)
为什么没有人使用堆栈溢出或谷歌的精彩搜索?
以下是您的回答:https://stackoverflow.com/a/3348532/312312
从同一个帖子来看,这似乎是一个更简单的解决方案: https://stackoverflow.com/a/8891030/312312
答案 1 :(得分:0)
这是最好的解决方案: 在您声明了tapRecognizer之后(我指的是您在问题上的代码)添加以下内容:
tapRecognizer.cancelsTouchesInView = NO;