我(如何)可以创建自己的UITouch事件,可以绑定到IBActions吗?

时间:2011-06-12 14:36:51

标签: iphone interface-builder uitouch

我有一个新的手势,(触摸,拖动到其他,释放),我希望其行为与touchUpInside或touchDown或在界面构建器中绑定IBActions时弹出的任何列表相同。 (即我希望我的自定义列表在列表中,以便我可以将它们绑定到IBActions)

有没有办法做到这一点,还是我需要以编程方式将事物联系在一起?

更新:下一个最好的事情

这是我所做的一个非常简化的版本,它将我的对象从任何其他特定类中解耦,以防其他人试图完成此任务:

@interface PopupButton : UIView {

}

//These are the replacement "IBAction" type connections.
@property (nonatomic) SEL tapAction;
@property (nonatomic, assign) id tapTarget;

@property (nonatomic) SEL dragAction;
@property (nonatomic, assign) id dragTarget;

@end

@implementation PopupButton

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //Your custom code to detect the event
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    //Your custom code to detect the event
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    //Here we'll say I detected the event I called "drag".
    if (!(self.dragTarget && self.dragAction)) {
        NSLog(@"No target for drag.");
        abort();
    } else {
        [self.dragTarget performSelector:self.dragAction];
    }

    //And here I detected the event "tap".
    if (!(self.tapTarget && self.tapAction)) {
        NSLog(@"No target for single tap.");
        abort();
    } else {
        [self.tapTarget performSelector:self.tapAction];
    }
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    //Your custom code to detect the event
}

@end

现在要设置它,在视图控制器中我想把这些操作绑定在一起代替:

- (void)viewDidLoad
{
    [equalsPopupButton setTapTarget:self];
    [equalsPopupButton setTapAction:@selector(equalsPress)];
    [equalsPopupButton setDragTarget:self];
    [equalsPopupButton setDragAction:@selector(isNamingResult)];
}

显然不如选项+拖动那么优雅,但它可以完成工作,而不会使我的代码变得僵硬和附加。希望这有助于某人。

1 个答案:

答案 0 :(得分:0)

无法以这种方式绑定功能。您应该在代码中使用辅助函数,并从IBaction函数和手势识别器函数调用它。