iOS UIActionSheet presented from LongPress gesture

2019-06-27 04:20发布

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条回答
Animai°情兽
2楼-- · 2019-06-27 05:07

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];
    } 
}
查看更多
登录 后发表回答