two action sheet showing up when using long press

三世轮回 提交于 2019-12-23 04:28:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!