iOS UIActionSheet 从按钮上的长按手势呈现,错误地需要双击按钮才能关闭

发布于 2024-12-12 05:31:07 字数 1497 浏览 0 评论 0原文

我有一个选项卡栏应用程序,在其中一个选项卡中我有一个 MKMapView。在此视图中,我的 viewDidLoad 正在初始化 UIButton 的长按手势识别器。当按下此按钮并帮助时,它会显示一个包含 5 个按钮 + 取消按钮的 UIActionSheet。每个按钮代表一个缩放级别:“世界”、“国家”、“州”、“城市”、“当前位置”。选择 UIActionSheet 中的按钮会将底层 MKMapView 缩放到该级别。

我遇到的问题是所有按钮(包括取消按钮)都需要双击才能关闭 UIActionSheet。这不是预期的行为——它应该像其他 UIActionSheet 一样在按下按钮一次后关闭。第一次按下后,我可以看到地图缩放到 UIActionSheet 后面的适当级别,因此我知道触摸正在正确的按钮上注册,但按钮在第一次按下时不会突出显示蓝色,并且 UIActionSheet 不会关闭。直到我第二次按下按钮,它才会突出显示蓝色,然后关闭。

如果我删除长按手势识别器并在“内部触摸”上显示 UIActionSheet,那么一切都会按预期进行。所以我知道这个手势在某种程度上造成了干扰,有什么修复或解决方法的想法吗?或者这是一个应该向 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];
}

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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾其所爱 2024-12-19 05:31:07

安娜·卡列尼娜是对的,链接 提供帮助我弄清楚。基本上,UILongPressGestureRecognizer 是一个经历各种状态变化的“连续手势”。我需要检查适当的状态,在我的例子中是 UIGestureRecognizerStateBegan,因为我希望在按住按钮之后但在释放并停止手势之前呈现 UIActionSheet。我所要做的就是将 UIActionSheet 的表示包装在检查适当状态的 if 语句中。现在它按预期工作了。

- (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];
    } 
}

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];
    } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文