如何检测 UIButton 按下状态? [关闭]

Posted

技术标签:

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

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

【问题讨论】:

按下状态是什么样的? 如果要改变Button的背景图片,我猜应该是Highlighted状态 @Hemal 检查这个:***.com/questions/11181340/… 没有按下按钮时我想连续移动另一个视图。当释放按钮按下时我想停止它。 您可以在 touchDown 上触发一种方法,并在 touchUp 中检测到触摸结束。 【参考方案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
;

【讨论】:

以上是关于如何检测 UIButton 按下状态? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

如何在按下 UIButton 的按钮时关闭按钮阴影?

如何检测音量上下是不是被按下

在 UITableView 顶部按下时,UIButton 不会更改其状态

按下 UIButton 后会弹出 UIDatePicker

如何在nib文件中按下时禁用UIButton高亮显示?

如何检测按下localNotification关闭按钮