【问题标题】:Can you disable a UIButton without disabling its attached UIGestureRecognizers?您可以在不禁用其附加的 UIGestureRecognizers 的情况下禁用 UIButton 吗?
【发布时间】:2012-02-08 14:02:26
【问题描述】:

我有一个具有 2 个功能的 UIButton:点击和长按。有点像 Springboard 应用程序(主屏幕)。我已经将它实现为一个 UIButton 并附加了一个 UILongPressGestureRecognizer。

在某些情况下,我需要通过在按钮上设置 enabled = NO 来禁用点击操作。但不幸的是,这也禁用了我的手势识别器。

如何禁用 UIButton 的标准行为,同时保持附加到它的手势识别器处于活动状态?

【问题讨论】:

    标签: iphone ios uibutton uigesturerecognizer


    【解决方案1】:

    UIButton 是 UIControl 的子类。 看看 UIControl 中的各种方法。
    您可以指定特定的方法来调用 ControlEvents(例如 ControlEventTouchUpInside 又名点击)和 UIEvents。

     - (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
    
     - (void)sendActionsForControlEvents:(UIControlEvents)controlEvents
    

    您可以通过以下方式删除控制事件的操作:

     - (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
    

    或者,根据您的条件,您可以在被调用的目标/选择器方法中不执行任何操作。

    【讨论】:

      【解决方案2】:

      您可以删除按钮的点击目标和选择器,然后在需要时再次添加。通过设置enabled = NO;,您将UIControl 的接收器设置为NO。

      更多文档 UIButton - Enabled

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-03-26
        • 1970-01-01
        • 2019-02-12
        • 1970-01-01
        • 1970-01-01
        • 2021-01-15
        • 1970-01-01
        相关资源
        最近更新 更多