标签:
UIkit框架中绝大多数的控件都是继承自,UIResponder类,UIResponder 类有强大的处理触摸事件的能力。假如一个UIview 收到一个触摸事件,那么这个触摸事件就会去进行寻找相应的响应事件,如果在该UIview 中找不到,就寻找UIView的对象去处理,如果UIView对象没有权利处理,就往当前的上一层UIViewController去寻找,如果找不到就再寻找 UIViewController 的对象去处理,如果这个对象仍然不能处理,就再往上层 UIWindow 对象去处理,如果它热不能解决触摸事件的响应,那该触摸事件就会被传递到 UIApplication 代理对象,如果该代理对象仍不能解决,那就交给系统回收。
总结一下:相当于在村里发生一件事,村长不能决定,这时就一级一级上报,可是一直没有得到处理,直到最后有个很大权力的人拍板决定,才算结束。要不就一直往上报。
系统将事件封装到 UIEvent 类中,然后由系统去处理。ios 将事件分为三种:触摸事件、动作事件、外部控制事件。动作事件:就是用户对手机进行的特定的动做,比如摇一摇;外部控制事件:就是控制手机连上手机设备时候的事件;触摸事件:就是用户与手机屏幕的相关事件。
每一个用户交互对象 UIResponder 都有一组响应事件函数。通常我们都要重写这组函数。以供我们使用相应的逻辑。
关于概念的知识这里就不再多说看博客网址(http://blog.csdn.net/yitailong/article/details/8228946)
基本代码实现:打印出鼠标的手势事件
建一个 UIView 的文件命名为 TouchView 在视图控制器里写上
#import "RootViewController.h" #import "TouchView.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; [self setTouchView]; } -(void)setTouchView{ TouchView * touchView = [[TouchView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)]; touchView.backgroundColor = [UIColor redColor]; [self.view addSubview:touchView]; UIButton * Button = [UIButton buttonWithType:UIButtonTypeCustom]; Button.frame = CGRectMake(20, 200, 280, 30); Button.backgroundColor = [UIColor grayColor]; [Button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [Button setTitle:@"点击跳转" forState:UIControlStateNormal]; [self.view addSubview:Button]; pinchView * View = [[pinchView alloc]initWithFrame:CGRectMake(50, 300, 100, 100)]; View.backgroundColor = [UIColor blackColor]; [self.view addSubview:View]; } -(void)buttonAction:(UIButton *)sender{ SecondViewController * SVC = [[SecondViewController alloc]init]; [self.navigationController pushViewController:SVC animated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
在 UIView.m文件里写上
#import "TouchView.h" @implementation TouchView -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self updateInfor:[touches anyObject] withMethodName:@"touchesBegin"]; } -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ [self updateInfor:[touches anyObject] withMethodName:@"touchesCancelled"]; } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ [self updateInfor:[touches anyObject] withMethodName:@"touchesEnded"]; } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ [self updateInfor:[touches anyObject] withMethodName:@"touchesMoved"]; } -(void)updateInfor:(UITouch *)aTouch withMethodName:(NSString *)aMethodName{ NSString * strPhase = @""; switch (aTouch.phase) { case UITouchPhaseBegan: strPhase = @"UITouchPhaseBegan"; break; case UITouchPhaseEnded: strPhase = @"UITouchPhaseEnded"; break; case UITouchPhaseCancelled: strPhase = @"UITouchPhaseCancelled"; break; case UITouchPhaseMoved: strPhase = @"UITouchPhaseMoved"; break; default: break; } NSLog(@"操作事件是 %@",strPhase); } @end
标签:
原文地址:http://www.cnblogs.com/benpaobadaniu/p/4926227.html