我可以(如何)创建自己的可以绑定到 IBActions 的 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)];
}
显然不如选项+拖动那么优雅,但它可以完成工作,而不会使我的代码变得僵化和附加。希望这对某人有帮助。
I have a new gesture, (touch, drag onto other, release) which I would like to have behave the same as touchUpInside or touchDown or any of that list that pops up when you tie in IBActions in interface builder. (I.e. I want my custom ones on that list so I can tie them to IBActions)
Is there a way to do this or do I need to programmatically tie things together?
Update: The Next Best Thing
Here's a very stripped down version of what I did that de-coupled my object from any other particular class, in case anyone else tries to accomplish this:
@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
Now to set this up, in the view controller I wanted to tie these actions I put this instead:
- (void)viewDidLoad
{
[equalsPopupButton setTapTarget:self];
[equalsPopupButton setTapAction:@selector(equalsPress)];
[equalsPopupButton setDragTarget:self];
[equalsPopupButton setDragAction:@selector(isNamingResult)];
}
Not as elegant obviously as option+drag, but it gets the job done without making my code all rigid and attached. Hope this helps someone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不可能以这种方式绑定功能。您应该在代码中使用辅助函数,并从 IBaction 函数和手势识别器函数中调用它。
It is not possible to tie the functions this way. You should use an auxiliary function in code and call it from the IBaction function and the gesture recognizer function.