3 回答
TA贡献1869条经验 获得超4个赞
在文档中,查找“ 响应者对象”和“响应者链”
您可以通过转发响应者链上的修饰来“共享”对象之间的修饰。您的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 {
printf("MyButton touch Began\n");
[self.nextResponder touchesBegan:touches withEvent:event];
}
@end
TA贡献1735条经验 获得超5个赞
我知道这个问题已经有两年了(并且已经回答了),但是...
当我自己尝试时,触摸被转发了,但是按钮不再像按钮那样表现。我也将修饰传递给“超级”,现在一切都很好。
因此,对于可能偶然发现此问题的初学者,代码应如下所示:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self.nextResponder touchesBegan:touches withEvent:event];
}
TA贡献1982条经验 获得超2个赞
也无需继承!只需将其放在实现的顶层即可,然后再进行其他操作:
#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
- 3 回答
- 0 关注
- 719 浏览
添加回答
举报