使用UIButton进行触摸检测

时间:2011-07-15 20:12:27

标签: iphone uibutton touch

我在使用UIButton操作和touchesmoved时遇到问题。场景是,当我将手指拖过一系列按钮时,应该调用它的动作。我也可以单独触摸按钮。

我使用touchesmoved方法找出uibutton上的触摸点以触发其动作。这种方法的问题是,当我点击视图中的某个地方并将手指移到按钮上时,一切正常,但如果我开始触摸UIButton本身,touchesmoved方法就不会被调用。

2 个答案:

答案 0 :(得分:3)

答案 1 :(得分:0)

如果您不需要知道触摸的确切位置,只需在它们上面滑动,就可以向其添加目标并使用许多UIControlEvent中的一个,如UIControlEventTouchDragInside等。

这是直接来自UIControl.h

的完整枚举
enum {
    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
};
typedef NSUInteger UIControlEvents;