识别iPhone上的捏手势拖动手势?

时间:2010-12-25 02:22:13

标签: iphone objective-c

我在视图上有一个小图像。视图是对象进程多点触摸操作。如果手指在视图上拖动,图像将平移其位置。如果用户使用2个手指进行捏合手势,图像将缩放其大小。我的工作如下面的部分代码:

//touch detect methods

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Touch began");

//Devide into 2 cases: 1 touch and 2 touches. 
if ([touches count] == 1) {
    NSLog(@"Touch began cout = 1");
    currentImageCenter = focusImage.center;
    UITouch *touch = [[touches allObjects] objectAtIndex:0];
    previousPoint = [touch locationInView:self];
    isTwoFingerTouching = FALSE;
}
else if([touches count] == 2){
    NSLog(@"Touch began cout = 2");
    UITouch *touch = [[touches allObjects] objectAtIndex:0];
    beginFirstPoint = [touch locationInView:self];
    touch = [[touches allObjects] objectAtIndex:1];
    beginSecondPoint = [touch locationInView:self];
    isTwoFingerTouching = TRUE;
}

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

if ([touches count] == 1 && isTwoFingerTouching == FALSE) {
    UITouch *touch = [touches anyObject]; 
    CGPoint currentPosition = [touch locationInView:self];
    //Calculate distance
    double deltaX = currentPosition.x - previousPoint.x;
    double deltaY = currentPosition.y - previousPoint.y;
    NSLog(@"Touch move detect 1 touch ");
    focusImage.center = CGPointMake(currentImageCenter.x+deltaX, currentImageCenter.y+deltaY); 
}
else if([touches count] == 2){
    NSLog(@"Touch move detect 2 touches");
    CGPoint currentFirstPoint;
    CGPoint currentSecondPoint;
    UITouch *touch = [[touches allObjects] objectAtIndex:0];
    currentFirstPoint = [touch locationInView:self];
    CGPoint previousFirstPoint = [touch previousLocationInView:self];
    touch = [[touches allObjects] objectAtIndex:1];
    currentSecondPoint = [touch locationInView:self];
    CGPoint previousSecondPoint = [touch previousLocationInView:self];

    //Compare previous points with current points. 
    //Pinch gesture
    CGFloat beginDistance = distanceBetweenPoints(previousFirstPoint, previousSecondPoint);
    CGFloat currentDistance = distanceBetweenPoints(currentFirstPoint, currentSecondPoint);
    if (currentDistance > 0 && beginDistance > 0) {
        double scale = currentDistance/beginDistance;
        NSLog(@"%f", scale);
        //Rotation
        CGPoint vector1 = CGPointMake(previousFirstPoint.x - previousSecondPoint.x, previousFirstPoint.y - previousSecondPoint.y);
        CGPoint vector2 = CGPointMake(currentFirstPoint.x - currentSecondPoint.x, currentFirstPoint.y - currentSecondPoint.y);
        //[vector1, vector2]. 
        double zValue = vector1.x*vector2.y - vector1.y*vector2.x;

        CGFloat rotateAngle =  angleBetweenLines(previousFirstPoint, previousSecondPoint, currentFirstPoint, currentSecondPoint);
        //zValue < 0, vector1 rotate counter-clockwise, so the angle should be negative.

        if (zValue < 0) {
            rotateAngle = -rotateAngle;
        }
        //Don't allow to zoom out if the image is too small
        if (scale > 1 || focusImage.frame.size.width > 30) {
            CGAffineTransform previousTransform = focusImage.transform;
            CGAffineTransform mixTransform =  CGAffineTransformConcat(CGAffineTransformMakeScale(scale, scale), CGAffineTransformMakeRotation(rotateAngle));
            focusImage.transform = CGAffineTransformConcat(previousTransform, mixTransform);
        }
    }
}
}

但问题是:我无法识别屏幕上是1指还是2指。当我触摸2个手指,并且我移动(用于旋转和缩放)这些手指时,[touches count]方法中的touchesMoved:仍然偶尔等于1。有谁经历过这个,请告诉我如何解决我的问题?

1 个答案:

答案 0 :(得分:0)

您没有使用UIGestureRecognizers的原因?如果您只使用UIPanGestureRecognizerUIPinchGestureRecognizer,它将为您节省大量工作。

无论如何,iOS总是表现得那样。除非您以完全相同的毫秒触摸两个手指,否则您将首先检测到一个手指触摸,然后检测两个手指触摸。如果您仍想使用-touchesBegan ...,-touchesMoved ......等方法,则需要在代码中使用某种取消机制。

相关问题