我有一个计算器应用程序,我想根据用户长时间触摸的计算器按钮显示不同的警报视图文本。我有很长的触摸代码。如何确定长触摸发生在哪个计算器按钮上?
-(void)handleLongTapGesture:(UITapGestureRecognizer *)sender{
if (sender.state == UIGestureRecognizerStateEnded) {
}
else if (sender.state == UIGestureRecognizerStateBegan){
// The alert view text should change, depending on which calculator button is long touched
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"I want the alert view text to change according to what calculator button is long touched"
message: @""
delegate: self
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[alert setTag:1];
[alert show];
}
}
// This code only produces a set of X-Y coordinates if the User touches between calculator buttons, not on the button itself
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
NSLog(@"location.x = %f", location.x);
NSLog(@"location.y = %f", location.y);
}
答案 0 :(得分:0)
两个选项:
为每个视图创建一个手势识别器,您需要检测手势,所有这些都使用相同的目标。然后,您只需使用识别器的view
属性即可找到手势发生的视图(或按钮)。
创建单个手势识别器(在要检测手势的视图/按钮的超级视图上),然后使用locationInView:
查找手势的位置,并hitTest:withEvent:
到找到具体的子视图。