9.3 触摸和手势:捏合手势

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

#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







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值