在iOS中UIView是继承于UIResponder的,而UIResponder是专门用来响应用户的操作处理各种事件的,包括触摸事件(Touch Events)、运动事件(Motion Events)、远程控制事件(Remote Control Events,如插入耳机调节音量触发的事件),而很多我们常用的类也继承于UIResponder(UIApplication、UIView、UIViewController).
而以下几个方法
@interface UIResponder : NSObject
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;//触摸屏幕
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;//在屏幕上移动
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;//离开屏幕
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
是响应触摸事件的方法,我们可以利用这几个方法自定义自己的手势。当然Apple也为我们提供了几个基础的封装的手势提供使用(了UIGestureRecognizer手势识别)
这里并不深入研究手势的响应和传递,而是研究下几个基础的手势和touchs的关系,这里主要利用这几个内置的手势方法:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapEvent:)];
[self addGestureRecognizer:tap];//点击
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(tapEvent:)];
[self addGestureRecognizer:pan];//平移,慢速移动
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(tapEvent:)];//滑动,快速移动
[self addGestureRecognizer:swipe];
UILongPressGestureRecognizer *longG = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(tapEvent:)];//长按
[self addGestureRecognizer:longG];
屏幕快照 2018-07-30 上午9.19.35.png
网友评论