ios - 如何检测 UIBarButtonItem 的 touchesbegan touchesended?

标签 ios touchesbegan

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

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

   /* now perform action */
}

现在我还想检测 fingerdown/fingerup,因为我想为 midi 合成器类型的应用程序触发 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++ 程序员,我对 objective-c 和 iphone 环境的了解非常有限。我只知道如何使用 UI 编辑器,我还不知道如何创建自定义 UI 并在没有此编辑器的情况下使用它们。

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

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

谢谢,

爸爸

最佳答案

你可以这样做(不需要继承 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 {
}

关于ios - 如何检测 UIBarButtonItem 的 touchesbegan touchesended?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8115103/

相关文章:

ios - Swift:如何创建/定义用户触摸图像的区域(宽度 x 高度)?

swift - 如何让 touch.locationInNode() 识别节点和它的子节点之间的区别?

ios - 如何将印度数字转换为阿拉伯数字?

ios - Airplay:在外部窗口上镜像 subview

ios - 发送到不可变对象(immutable对象)的变异方法关闭应用程序

ios - 在 swift 中发布执行闭包 block

ios - 未调用调度组功能 - 如何使用调度组?

swift - touchesBegan 在静态 tableView 中未被调用

ios - UITouch-是否可以禁用多次点击

objective-c - 使用 IBAction 而不是 touchesBegan :