我可以如何创建自己的可以与IBAction绑定的UITouch事件吗?

问题描述

| 我有一个新手势(触摸,拖动到其他手势,松开),它的行为与touchUpInside或touchDown或当您在界面构建器中绑定IBAction时弹出的任何列表相同。 (即,我希望我的自定义项在该列表中,以便将它们与IBAction绑定在一起) 有没有办法做到这一点,或者我需要以编程方式将事物捆绑在一起吗? 更新:下一件事 这是我所做的工作的精简版本,它使我的对象与任何其他特定的类分离,以防其他人尝试实现此目的:
@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)];
}
显然不如Option + Drag那样优雅,但是它可以完成工作而无需使我的代码变得僵硬和附加。希望这对某人有帮助。     

解决方法

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...