9.3 触摸和手势:捏合手势

来源:互联网 发布:网络个人研修计划 编辑:程序博客网 时间:2024/04/30 05:19

==========模拟捏合手势===========

#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







0 0
原创粉丝点击