【问题标题】:How to detect UIButton pressing state? [closed]如何检测 UIButton 按下状态? [关闭]
【发布时间】:2014-12-17 05:59:13
【问题描述】:

我想在按下按钮时连续调用方法。我可以使用 touchup inside 或其他类似操作来检测 UIButton 按下状态。那么如何检测 UIButton 的按下状态呢?

【问题讨论】:

  • 按下状态是什么样的?
  • 如果要改变Button的背景图片,我猜应该是Highlighted状态
  • 没有按下按钮时我想连续移动另一个视图。当释放按钮按下时我想停止它。
  • 您可以在 touchDown 上触发一种方法,并在 touchUp 中检测到触摸结束。

标签: ios iphone ipad uibutton


【解决方案1】:

我是这样做的:

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressRecordButton:)];
[self.view.recordBtn addGestureRecognizer:longPress];
UIButton *recordButton = self.view.recordBtn;
[recordButton addTarget:self action:@selector(beginTouchRecord:) forControlEvents:UIControlEventTouchDown];
[recordButton addTarget:self action:@selector(endTouch:) forControlEvents:UIControlEventTouchUpInside];
[recordButton addTarget:self action:@selector(endTouch:) forControlEvents:UIControlEventTouchCancel];

【讨论】:

    【解决方案2】:

    您只能获取此状态的操作方法

    typedef NS_OPTIONS(NSUInteger, UIControlEvents) {
    UIControlEventTouchDown           = 1 <<  0,      // on all touch downs
    UIControlEventTouchDownRepeat     = 1 <<  1,      // on multiple touchdowns (tap count > 1)
    UIControlEventTouchDragInside     = 1 <<  2,
    UIControlEventTouchDragOutside    = 1 <<  3,
    UIControlEventTouchDragEnter      = 1 <<  4,
    UIControlEventTouchDragExit       = 1 <<  5,
    UIControlEventTouchUpInside       = 1 <<  6,
    UIControlEventTouchUpOutside      = 1 <<  7,
    UIControlEventTouchCancel         = 1 <<  8,
    
    UIControlEventValueChanged        = 1 << 12,     // sliders, etc.
    
    UIControlEventEditingDidBegin     = 1 << 16,     // UITextField
    UIControlEventEditingChanged      = 1 << 17,
    UIControlEventEditingDidEnd       = 1 << 18,
    UIControlEventEditingDidEndOnExit = 1 << 19,     // 'return key' ending editing
    
    UIControlEventAllTouchEvents      = 0x00000FFF,  // for touch events
    UIControlEventAllEditingEvents    = 0x000F0000,  // for UITextField
    UIControlEventApplicationReserved = 0x0F000000,  // range available for application use
    UIControlEventSystemReserved      = 0xF0000000,  // range reserved for internal framework use
    UIControlEventAllEvents           = 0xFFFFFFFF
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-19
      • 1970-01-01
      • 2011-11-05
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多