当一个或多个手指触碰屏幕时
- (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;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//--------------------单击、双击----------------------
UITouch *touch = [touches anyObject];//touches里相当于有几个手指触摸
NSUInteger tapCount = touch.tapCount;//触摸的次数
if (tapCount == 1) {//如果单机
[self performSelector:@selector(singleTap) withObject:nil afterDelay:0.5];// 0.5秒以后执行
}
else if(tapCount == 2) {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap) object:nil];// 取消执行单机
[self doubleTap];
}
CGPoint point = [touch locationInView:self];//获取手指在当前view上的坐标
}
当用户触摸屏幕时,事件会被封装成一个event实例,包含了触摸的相关信息(位置信息),event实例中包含着若干个UITouch信息,一个touch相当于一个手指,touchs的数量
相当于用户手指的数量;
UITouch的常用属性:
window:触摸产生时所在的窗口
view:触摸产生时所在的view
tapCount:短时间内点击的次数,可以用于判断单击和双击
timestamp:触摸事件产生或变化的时间,单位为秒
phase:触摸事件在触摸周期中所处的状态:包括:
UITouchPhaseBegan, // 开始触摸.
UITouchPhaseMoved, // 触摸点移动
UITouchPhaseStationary, // 触摸点无移动
UITouchPhaseEnded, // 触摸结束
UITouchPhaseCancelled, // 触摸取消
UITouch常用方法:
CGPoint point = [touch locationInView:self];//获取这次触摸的坐标点
CGPoint point2 = [touch previousLocationInView:self];//获取上一个触摸的坐标点
捏合事件:
要实现多点触摸必须使此属性为yes
//开启多点触摸
self.multipleTouchEnabled = YES;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touches count] == 2) {
NSArray *touchArray = [touches allObjects]; //获取触摸点个数
UITouch *firstTouch = [touchArray objectAtIndex:0]; //取出第一个触摸点
UITouch *secondTouch = [touchArray objectAtIndex:1]; //取出第二个触摸点
// secondTouch.tapCount;//获取点击次数
CGPoint point1 = [firstTouch locationInView:self];//获取第一个触摸点所在的位置
CGPoint point2 = [secondTouch locationInView:self];;//获取第二个触摸点所在的位置
double distance = [self distance:point1 point:point2];//计算两点间的距离
NSLog(@"%f",distance);
}
}
//计算两点之间的距离
- (double)distance:(CGPoint)p1 point:(CGPoint)p2 {
// ((x1-x2)平方+(y1-y2)平方)开方
double distnce = sqrt(pow(p1.x-p2.x, 2)+pow(p1.y-p2.y, 2));
return distnce;
}
事件响应者链
UIApplication UIView UIWindow UIViewController都使直接或间接继承UIResponser,所以这些类的实例都是响应者对象
响应者链表示一系列的响应者对象。事件由UIApplication->UIWindow->...->第一响应者(被直接点击的对象),事件将由第一响应者来处理,如果
第一相应着处理不了,或第一相应者不处理,将交给下一个响应者(它的父视图)来处理,最后传到UIApplication,如果不处理,事件丢弃;
- (void)viewDidLoad
{
[super viewDidLoad];
//----------------------点击手势-------------------------
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[self.view addGestureRecognizer:tap];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];
//区别单击、双击手势
[tap requireGestureRecognizerToFail:doubleTap];
//----------------------轻扫手势-------------------------
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeGesture];
//----------------------滑动手势-------------------------
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[self.view addGestureRecognizer:panGesture];
//----------------------长按手势-------------------------
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
longPress.minimumPressDuration = 2;
[self.view addGestureRecognizer:longPress];
//----------------------旋转手势-------------------------
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[self.view addGestureRecognizer:rotation];
//----------------------捏合手势-------------------------
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[self.view addGestureRecognizer:pinch];
}
- (void)tap:(UITapGestureRecognizer *)tapGetrue {
NSLog(@"单击");
}
- (void)doubleTap:(UITapGestureRecognizer *)tapGetrue {
NSLog(@"双击");
}
- (void)swipeAction:(UISwipeGestureRecognizer *)swipeGesture {
NSLog(@"轻扫");
}
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
if (longPress.state == UIGestureRecognizerStateEnded) {
return;
}
NSLog(@"长按");
}
- (void)panAction:(UIPanGestureRecognizer *)panGesture {
CGPoint p = [panGesture locationInView:self.view];
NSLog(@"%@",NSStringFromCGPoint(p));
}
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation {
//角度
float degree = rotation.rotation*(180/M_PI);
NSLog(@"%f",degree);
}
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch {
if (pinch.scale > 0) {
NSLog(@"放大");
} else {
NSLog(@"缩小");
}
NSLog(@"%f",pinch.scale);
}
滑动手势
CGPoint translatedPoint = [panGesture translationInView:self.view];
NSLog(@"%f",translatedPoint.x);
translatedPoint.x小于0向做滑动,大于0向右滑动