我有几个UIButton
用于在主区域中点击时设置当前操作的s.我还想让用户直接从按钮拖动到主区域并采取相同的动作; 实质上,touchesBegan和touchesMoved应该在触摸UIButtons时传递到主视图,但也应该发送按钮按下动作.
现在,我在内部修改控制.拖动出口调用内部部分设置控件,然后调用触摸开始部分启动主区域触摸操作.
但是,此时,touchesMoved和touchesEnded显然没有被调用,因为触摸起源于UIButton.
有没有办法半忽略触摸所以它们被传递到主区域,但也允许我先设置控件?
我知道这个问题已经两年了(已经回答了),但是......
当我自己尝试这个时,触摸被转发,但按钮不再像按钮那样.我也通过了"超级"的接触,现在一切都很顺利.
因此,对于可能偶然发现的初学者来说,这就是代码应该是这样的:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; [self.nextResponder touchesBegan:touches withEvent:event]; }
在文档中,查找响应程序对象和响应程序链
您可以通过转发响应链上的触摸来"共享"对象之间的触摸.你的UIButton有一个接收UITouch事件的响应者/控制器,我的猜测是,一旦它对它返回的消息进行了解释 - 触摸已被处理和处理.
Apple建议这样的事情(基于触摸的类型):
[self.nextResponder touchesBegan:touches withEvent:event];
传递的不是处理触摸事件.
子类UIButton:
MyButton.h
#import@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
不需要子类化!在其他任何事情之前,只需将其放在实现的顶部:
#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