通过滑动手指按下按钮(用于钢琴效果)

时间:2013-06-08 03:53:53

标签: ios objective-c

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

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

3 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

我认为只需在按钮上处理触摸事件就无法实现您想要的效果。我建议使用UIPanGestureRecognizer来处理用户的滑动手指。你可以检查哪个按钮被刷过,然后播放相应的音乐。

答案 2 :(得分:0)

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