苹果官方给的响应链示意图如下所示:
用文字描述的话就是:
subView->UIView->(UIViewController)->UIWindow->UIApplication->UIApplicationDelegate
自定义一个视图,重写touchesBegan:withEvent:方法:
#import <UIKit/UIKit.h>
@interface MyTouch : UIView
@end
@implementation MyTouch
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
NSLog(@"This is my touch!");
}
@end
在ViewController的view中添加一个MyTouch视图:
- (void)viewDidLoad {
[super viewDidLoad];
MyTouch *my = [[MyTouch alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
my.backgroundColor = [UIColor redColor];
[self.view addSubview:my];
}
建立符号断点,并在断点处打印$arg1的值:
符号断点
运行项目,查看打印日志:
<MyTouch: 0x7fbba5e17b10; frame = (0 0; 100 100); layer = <CALayer: 0x60400002e900>>
<UIView: 0x7fbba5e18400; frame = (0 0; 375 812); autoresize = W+H; layer = <CALayer: 0x604000232bc0>>
<ViewController: 0x7fbba5d058f0>
<UIWindow: 0x7fbba5f02f20; frame = (0 0; 375 812); gestureRecognizers = <NSArray: 0x600000251310>; layer = <UIWindowLayer: 0x60400003ad00>>
<UIApplication: 0x7fbba5e00000>
<AppDelegate: 0x600000036240>
2018-12-20 17:22:21.361654+0800 Current[41380:15485776] This is my touch!
可以看到响应链为MyTouch->UIView->ViewController->UIWindow->UIApplication->AppDelegate,与文档描述一致。
网友评论