如何检测touchesbegan touchesended为UIBarButtonItem?

时间:2011-11-13 22:02:27

标签: ios touchesbegan

我正在使用UIBarButtonItem来触发事件。我在xcode4中使用集成的InterfaceBuider来创建我的UIBarButtonItem,然后将按钮连接到视图控制器中的方法。该方法如下所示:

-(IBAction)NoteOnOff:(id)sender
{
    UIButton *button = (UIButton*)sender;

   /* now perform action */
}

现在我还要检测fingerdown / fingerup,因为我想为midi synth类型的app触发noteon / noteoff类型的事件。

1-有没有办法检测上述方法中是否按下了sender

2-我尝试将UIBarButtonItem子类化并以这种方式实现touchesBegan和touchesEnded:

@interface myUIBarButtonItem : UIBarButtonItem {

}

@end

@implementation myUIBarButtonItem

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesBegan");
NSLog(@"touches=%@,event=%@",touches,event);
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesEnded");
NSLog(@"touches=%@,event=%@",touches,event);
}

@end

然后我在界面编辑器中将UIBarButtonItem的类更改为myUIBarButtonItem,但没有运气。这是在界面编辑器中使用我的自定义类的正确方法吗?

3-我在某地读过UIBarButtonItem不从UIResponder继承,因此他们无法拦截touchesbegan / touchesended事件。如果是这种情况,那么能够检测到触摸并触及事件的正确方法是什么?我主要是一名C / C ++程序员,而且我对于客观C和iphone环境的知识非常有限。我只知道如何使用UI编辑器,我不知道如何创建自定义UI并在没有此编辑器的情况下使用它们。

底线是:检测触地/触摸的最简单方法是什么,以尽可能减少延迟?

也欢迎指向教程或文档的任何指针。

谢谢,

巴巴

1 个答案:

答案 0 :(得分:-2)

你可以这样做(不需要子类UIBarButtonItem):

[button addTarget:self action:@selector(touchUp:)
  forControlEvents:UIControlEventTouchUpInside];

[button addTarget:self action:@selector(touchDown:)
  forControlEvents:UIControlEventTouchDown];

- (void) touchUp:(id)sender {
}

- (void) touchDown:(id)sender {
}