NSMutableSet * mutableTouches = [touches mutableCopy];
//或者
NSSet *allTouches = [event allTouches];
2.判断是多触点还是单触点
NSSet *allTouches = [event allTouches];
[allTouches count] = 1 为单触点
[allTouches count] >= 2 为多触点。同时可以知道是几个触点
//第一触点
UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
//第二触点
UITouch *touch = [[allTouches allObjects] objectAtIndex:1];
//第三触点
UITouch *touch = [[allTouches allObjects] objectAtIndex:2];
判定触摸点是单击还是双击 [touch tapCount] 决定
/
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
//单击,双击处理
NSSet *allTouches = [event allTouches];
switch ([allTouches count]) {
case 1: {
UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
switch ([touch tapCount]) {
case 1: //单击
break;
case 2: //双击
break;
default:
break;
}
}
break;
default:
break;
}
}
//计算两个点之间的距离函数
- (CGFloat)distanceBetweenTowPoints:(CGPoint)fromPoint toPoint:(CGPoint)toPoint {
float x = toPoint.x - fromPoint.x;
float y = toPoint.y - fromPoint.y;
return sqrt(x * x + y * y);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//两个指头的分开,合拢手势
NSSet * allTouches = [event allTouches];
switch ([allTouches count]) {
case 1: //单点触摸
break;
case 2: {//两点触摸
UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0];
UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1];
beginDistance = [self distanceBetweenTowPoints:[touch1 locationInView:[self view]] toPoint:[touch2 locationInView:[self view]]];
}
break;
default:
break;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
//两个指头移动时,判断是分开还是合拢
NSSet *allTouches = [event allTouches];
switch ([allTouches count]) {
case 1:
break;
case 2: {
UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0];
UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1];
CGFloat endDistance = [self distanceBetweenTowPoints:[touch1 locationInView:[self view]] toPoint:[touch2 locationInView:[self view]]];
if (beginDistance > endDistance) {
//合拢
}
else {
//分开
}
}
break;
default:
break;
}
}