问题
Inside my viewDidLoad, I have the following:
UILongPressGestureRecognizer *longpressGesture =[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongpressGesture:)];
longpressGesture.minimumPressDuration = 1;
longpressGesture.allowableMovement = 5;
longpressGesture.numberOfTouchesRequired = 1;
[self.tableView addGestureRecognizer:longpressGesture];
[longpressGesture release];
I created the following:
-(IBAction) handleLongpressGesture:(UIGestureRecognizer *) sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Delete Record?" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Yes",@"No",nil];
[actionSheet showInView:self.view];
[actionSheet release];
}
Using the simulator, when I long press, two action sheet shows up instead of the one.
Any ideas as to why this is so?
Is it a problem with the simulator?
回答1:
It's not a problem with the simulator.
The gesture handler is called multiple times as the gesture goes through different states (began, ended, etc).
You need to check the gesture's state
in the handler method:
-(IBAction) handleLongpressGesture:(UIGestureRecognizer *) sender {
if (sender.state == UIGestureRecognizerStateBegan)
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] init...
[actionSheet showInView:self.view];
[actionSheet release];
}
}
来源:https://stackoverflow.com/questions/7757124/two-action-sheet-showing-up-when-using-long-press