==========模拟捏合手势===========
#import "TouchView.h"
@interface TouchView ()
{
double lastDistance;
}
@end
@implementation TouchView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 开启触摸响应,默认是yes
self.userInteractionEnabled = YES;
// 开启多点触摸,默认NO;
self.multipleTouchEnabled = YES;
}
return self;
}
- (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];
CGPoint point1 = [firstTouch locationInView:self];
CGPoint point2 = [secondTouch locationInView:self];
double distance = [self distance:point1 point:point2];
NSLog(@"%f", distance);
}
}
// 模拟捏合手势
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count] == 2) {
NSArray *touchArray = [touches allObjects];
UITouch *firstTouch = [touchArray objectAtIndex:0];
UITouch *secondTouch = [touchArray objectAtIndex:1];
CGPoint point1 = [firstTouch locationInView:self];
CGPoint point2 = [secondTouch locationInView:self];
//当前两点的距离
double distance = [self distance:point1 point:point2];
float subValue = distance - lastDistance;
if (subValue > 0) {
NSLog(@"放大捏合");
}else {
NSLog(@"缩小捏合");
}
lastDistance = distance;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
// 求出两点距离
- (double)distance:(CGPoint)p1 point:(CGPoint)p2
{
// ((x1-x2)平方+(y1-y2)平方)开方
double distance = sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
return distance;
}
@end