有没有办法在iPhone上通过触摸?

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

现在,我可以更改控件了。拖动出口将调用内部触摸区域以设置控件,然后调用触摸开始区域以开始主区域触摸操作。

但是,此时,touchesMoved和touchesEnded显然没有被调用,因为触摸起源于UIButton。

有没有一种方法可以半忽略触摸,以便将它们传递到主区域,还可以让我先设置控件?


慕慕森
浏览 652回答 3
3回答

MMTTMM

在文档中,查找“ 响应者对象”和“响应者链”您可以通过转发响应者链上的修饰来“共享”对象之间的修饰。您的UIButton有一个接收UITouch事件的响应器/控制器,我的猜测是,一旦它对返回的消息执行了正确的解释-触摸已被处理和处置。苹果公司建议这样的事情(当然取决于触摸的类型):[self.nextResponder touchesBegan:touches withEvent:event];而不是处理触摸事件,而是将其传递出去。子类UIButton:MyButton.h#import <Foundation/Foundation.h>@interface MyButton : UIButton {}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event ;@end我的按钮#import "MyButton.h"@implementation MyButton- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {&nbsp;&nbsp;&nbsp; &nbsp; printf("MyButton touch Began\n");&nbsp; &nbsp; [self.nextResponder touchesBegan:touches withEvent:event];&nbsp;}@end

喵喔喔

我知道这个问题已经有两年了(并且已经回答了),但是...当我自己尝试时,触摸被转发了,但是按钮不再像按钮那样表现。我也将修饰传递给“超级”,现在一切都很好。因此,对于可能偶然发现此问题的初学者,代码应如下所示:- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {&nbsp;&nbsp;&nbsp; &nbsp; [super touchesBegan:touches withEvent:event];&nbsp; &nbsp; [self.nextResponder touchesBegan:touches withEvent:event];&nbsp;}

临摹微笑

也无需继承!只需将其放在实现的顶层即可,然后再进行其他操作:#pragma mark PassTouch@interface UIButton (PassTouch)@end@implementation UIButton (PassTouch)- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{&nbsp; &nbsp; [super touchesBegan:touches withEvent:event];&nbsp; &nbsp; [self.nextResponder touchesBegan:touches withEvent:event];}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{&nbsp; &nbsp; [super touchesMoved:touches withEvent:event];&nbsp; &nbsp; [self.nextResponder touchesMoved:touches withEvent:event];}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{&nbsp; &nbsp; [super touchesEnded:touches withEvent:event];&nbsp; &nbsp; [self.nextResponder touchesEnded:touches withEvent:event];}- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{&nbsp; &nbsp; [super touchesCancelled:touches withEvent:event];&nbsp; &nbsp; [self.nextResponder touchesCancelled:touches withEvent:event];}@end
打开App,查看更多内容
随时随地看视频慕课网APP