iOS UIActionSheet presented from LongPress gesture on a button erroneously requires double clicking buttons to dismiss

狂风中的少年 提交于 2019-12-23 12:33:55

问题


I have a tabbar application, in one of the tabs I have a MKMapView. In this view, my viewDidLoad I am initializing a long press gesture recognizer for a UIButton. When this button is pressed and help it presents a UIActionSheet with 5 buttons + the cancel button. Each button represents a zoom level: "World", "Country", "State", "City", "Current Location". Selecting a button in the UIActionSheet zooms the underlying MKMapView to that level.

The problem I am having is that all of the buttons (including the cancel button) require double-tapping to dismiss the UIActionSheet. This is not the intended behavior -- it should dismiss after pressing the button once like every other UIActionSheet. After the first press I can see the map zooming to the appropriate level behind the UIActionSheet so I know the touch is registering on the correct button, but the button does not highlight blue upon the first press and the UIActionSheet does not dismiss. Not until I press the button for a second time does it highlight blue and then dismiss.

If I remove the longpress gesture recognizer and present the UIActionSheet on a 'touch up inside' then everything works as it is supposed to. So I know the gesture is somehow interfering, any ideas on a fix or workaround? Or is this a bug that should be reported to Apple?

- (void) viewDidLoad {
    // intitialize longpress gesture
    UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc]
                                                         initWithTarget:self 
                                                         action:@selector(zoomOptions:)];
    longPressRecognizer.minimumPressDuration = 0.5;
    longPressRecognizer.numberOfTouchesRequired = 1;
    [self.currentLocationButton addGestureRecognizer:longPressRecognizer];
}

- (IBAction) zoomOptions:(UIGestureRecognizer *)sender {
    NSString *title = @"Zoom to:";
    UIActionSheet *zoomOptionsSheet = [[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"World", @"Country", @"State", @"City", @"Current Location", nil];  

    [zoomOptionsSheet showFromTabBar:appDelegate.tabbarController.tabBar];
}

回答1:


Anna Karenina was right, and the link provided helped me figure it out. Basically, UILongPressGestureRecognizer is a "continuous gesture" which undergoes various state changes. I needed to check for the appropriate state, which in my case is UIGestureRecognizerStateBegan since I want the UIActionSheet presented after holding the button down but before you release and stop the gesture. All I had to do was wrap the presentation of the UIActionSheet in an if statement that checked for the appropriate state. Now it works as expected.

- (IBAction) zoomOptions:(UILongPressGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateBegan) {
        NSString *title = @"Zoom to:";
        UIActionSheet *zoomOptionsSheet = [[UIActionSheet alloc] 
                                            initWithTitle:title 
                                                 delegate:self 
                                        cancelButtonTitle:@"Cancel"
                                   destructiveButtonTitle:nil 
                                        otherButtonTitles:@"World", @"Country",                
                                                          @"State", @"City", 
                                                          @"Current Location", nil];  
        [zoomOptionsSheet showFromTabBar:appDelegate.tabbarController.tabBar];
    } 
}


来源:https://stackoverflow.com/questions/7907671/ios-uiactionsheet-presented-from-longpress-gesture-on-a-button-erroneously-requi

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