有没有办法在 iPhone 上传递触摸?

发布于 2024-11-10 09:07:22 字数 323 浏览 1 评论 0原文

我有几个 UIButton,用于在主区域中点击时设置当前操作。我还想允许用户从按钮直接拖动到主区域并执行相同的操作;本质上,触摸 UIButtons 时,touchesBegan 和 TouchesMoved 应该传递到主视图,但也应该发送按钮按下动作。

现在,我正在修改控制内部。拖动出口调用touch up inside部分来设置控件,然后调用touches begin部分来启动主区域的触摸操作。

然而,此时,touchesMoved 和touchesEnded 显然没有被调用,因为触摸源自UIButton。

有没有办法半忽略触摸,以便它们传递到主区域,但也允许我先设置控件?

I have several UIButtons which I use to set the current action when tapping in the main area. I would also like to allow the user to drag from the button directly into the main area and take the same action; essentially, the touchesBegan and touchesMoved should be passed on to the main view when touching the UIButtons, but also should send the button press action.

Right now, I have the touch up inside changing the control. The drag exit calls the touch up inside section to set the control, then calls the touches began section to start the main area touching operation.

However, at this point, the touchesMoved and touchesEnded are obviously not being called, because the touches originated on the UIButton.

Is there a way to half-ignore the touches so they're passed to the main area, but also allow me to set the control first?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

知你几分 2024-11-17 09:07:22

我知道这个问题已经有两年了(并且已经回答了),但是尽管如此......

当我自己尝试这个时,触摸被转发,但按钮的行为不再像按钮。我也将触摸传递给“超级”,现在一切都很好。

因此,对于可能偶然发现这一点的初学者来说,代码应该如下所示:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
    [super touchesBegan:touches withEvent:event];
    [self.nextResponder touchesBegan:touches withEvent:event]; 
}

I know this question is two years old (and already answered), but nevertheless...

When I tried this myself, the touches were forwarded, but the buttons no longer behaved like buttons. I also passed the touches along to "super" and now all is well.

So, for beginners that might stumble upon this, this is what the code should look like:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
    [super touchesBegan:touches withEvent:event];
    [self.nextResponder touchesBegan:touches withEvent:event]; 
}
自我难过 2024-11-17 09:07:22

在文档中,查找 响应者对象和响应者链

您可以通过将触摸转发到响应者链来在对象之间“共享”触摸。
您的 UIButton 有一个接收 UITouch 事件的响应器/控制器,我的猜测是,一旦它执行了对其返回的消息的解释 - 触摸已被处理和处置。

苹果建议这样的东西(当然基于触摸的类型):

[self.nextResponder attemptsBegan:touches withEvent:event];

而不是处理触摸事件过去了。

子类 UIButton:

MyButton.h

#import <Foundation/Foundation.h>

@interface MyButton : UIButton {

}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event ;

@end

MyButton.m

#import "MyButton.h"

@implementation MyButton

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
    printf("MyButton touch Began\n");
    [self.nextResponder touchesBegan:touches withEvent:event]; 
}
@end

In the documentation, look for Responder Objects and the Responder Chain

You can "share" touches between objects by forwarding the touch up the responder chain.
Your UIButton has a responder/controller that receives the UITouch events, my guess is that once it has preformed its interpretation of the message it returns - the touch has been handled and disposed of.

Apple suggests something like this (based on the type of touch of course):

[self.nextResponder touchesBegan:touches withEvent:event];

Rather than disposing of the touch event it is passed on.

Sub classed UIButton:

MyButton.h

#import <Foundation/Foundation.h>

@interface MyButton : UIButton {

}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event ;

@end

MyButton.m

#import "MyButton.h"

@implementation MyButton

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
    printf("MyButton touch Began\n");
    [self.nextResponder touchesBegan:touches withEvent:event]; 
}
@end
下壹個目標 2024-11-17 09:07:22

也不需要子类化!只需将其放在您的实现的顶部即可:

#pragma mark PassTouch

@interface UIButton (PassTouch)
@end

@implementation UIButton (PassTouch)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
    [self.nextResponder touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    [self.nextResponder touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event];
    [self.nextResponder touchesCancelled:touches withEvent:event];
}
@end

No need to subclass either! Simply stick this on the top of your implementation before anything else:

#pragma mark PassTouch

@interface UIButton (PassTouch)
@end

@implementation UIButton (PassTouch)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
    [self.nextResponder touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    [self.nextResponder touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event];
    [self.nextResponder touchesCancelled:touches withEvent:event];
}
@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文