1

我正在创建一个类似钢琴的应用程序,其中每个按钮都会播放一个音符。我的 UIButtons 在 Touch Down 事件中播放声音。当我将手指从一个按钮滑到另一个按钮时出现问题(想象用手指在钢琴键上滑动以捕捉其间的所有音符),除非我抬起手指,否则它仍然“按住”第一个按钮而不触摸另一个按钮。

如何在不抬起手指的情况下捕捉多个 Touch Down 事件?

4

3 回答 3

1

您可以像这样向按钮添加其他目标:

myButton addTarget:self 
             action:@selector(yourMethodHere:withEvent: )
   forControlEvents:UIControlEventTouchDragInside];

以下是您可以使用的事件类型列表:

enum {
   UIControlEventTouchDown           = 1 <<  0,
   UIControlEventTouchDownRepeat     = 1 <<  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,

   UIControlEventEditingDidBegin     = 1 << 16,
   UIControlEventEditingChanged      = 1 << 17,
   UIControlEventEditingDidEnd       = 1 << 18,
   UIControlEventEditingDidEndOnExit = 1 << 19,

   UIControlEventAllTouchEvents      = 0x00000FFF,
   UIControlEventAllEditingEvents    = 0x000F0000,
   UIControlEventApplicationReserved = 0x0F000000,
   UIControlEventSystemReserved      = 0xF0000000,
   UIControlEventAllEvents           = 0xFFFFFFFF
};

然后你可以有这样的功能:

-(void) yourMethodHere:(UIControl *) button withEvent:(UIEvent *)event {
    //do whatever
}
于 2013-06-08T04:40:50.823 回答
0

我认为仅在按钮上处理触摸事件是无法实现您想要的。我建议用来UIPanGestureRecognizer处理用户的滑动手指。并且可以查看哪个按钮被刷过,然后播放相应的音乐。

于 2013-06-08T04:34:50.277 回答
0

本关于手势识别器的教程将对您有所帮助: http ://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more

于 2013-06-08T04:37:09.453 回答