root.m
#import "RootViewController.h" #import "MyView.h" @interface RootViewController () { UITextField *tf; } @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; tf = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, 300, 60)]; tf.backgroundColor=[UIColor redColor]; [self.view addSubview:tf]; MyView *view=[[MyView alloc]initWithFrame:CGRectMake(20, 300, 270, 200)]; view.backgroundColor=[UIColor grayColor]; // 关闭用户交互 view.userInteractionEnabled=NO; [self.view addSubview:view]; } // 触摸屏幕触发的方法 -(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"触发控制器开始"); } -(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ NSLog(@"取消控制器触摸"); } -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"触摸控制器结束"); } -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"移动控制器"); } @end
MyView.m
#import "MyView.h" @implementation MyView //若注释掉 则会有控制器方法代替 一次传递 // 触摸屏幕触发的方法 -(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"触发view开始"); } -(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ NSLog(@"取消view触摸"); } -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"触摸view结束"); } -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"移动view"); } @end