iOS实现多点触控

一、开启多点触控

  - (void)viewDidLoad {
    [super viewDidLoad];
    /**
     *  开启多点触控
     */
    self.view.multipleTouchEnabled = YES;
}

二、设置两点触控,对图片进行放大、缩小处理

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //每次touch开始给最终距离赋初值
    lastdistance = 0.0;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    //如果是2点触控,则进行缩放处理
    if (touches.count == 2 ) {
        CGPoint p1 = [touches.allObjects[0] locationInView:self.view];
        CGPoint p2 = [touches.allObjects[1] locationInView:self.view];
        float dx = p1.x - p2.x;
        float dy = p1.y - p2.y;
        float currentDistance = sqrtf(powf(dx, 2.0)+powf(dy, 2.0));
        if (lastdistance == 0 ) {
        
            lastdistance = currentDistance;
            
        }else{
        
            if (lastdistance - currentDistance > 0) {
            
                lastdistance = currentDistance;
                _imageView.transform = CGAffineTransformScale(_imageView.transform, 0.92, 0.92);
                NSLog(@"缩小");
                
            }else{
            
                NSLog(@"放大");
                _imageView.transform = CGAffineTransformScale(_imageView.transform, 1.07, 1.07);
                lastdistance = currentDistance;
            
            }
        
        }
        
    }

}


赞一个 (0)
分享到: +More

评论 沙了个发

换个身份

取消评论