如何基于Pan手势放大/缩小UIView?

时间:2014-03-12 11:06:59

标签: ios iphone uigesturerecognizer

我想根据UIView放大/缩小UIPanGesture。我不知道如何将translationInView转换为Scaling参数。 (我知道如何根据捏合手势放大/缩小。)

我试图弄清楚如何做到这一点,但到目前为止还没有这样的运气。

1 个答案:

答案 0 :(得分:1)

要进行缩放,您需要UIPinchGestureRecognizer,而不是Pan

- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer {    
    recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
    recognizer.scale = 1;    
}

同时检查this链接。

更新:

从UIPanGestureRecognizer接收触摸信息的唯一方法是

- (NSUInteger)numberOfTouches;
- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView*)view;

所以也许你可以尝试这样的事情:

    CGFloat old_distance = 0.0; // keep it somewhere between touches!

- (void)panGestureRecognized:(UIPanGestureRecognizer *)recognizer {
    if (recognizer.numberOfTouches == 2) {
        CGPoint a = [recognizer locationOfTouch:0 inView:recognizer.view];
        CGPoint b = [recognizer locationOfTouch:1 inView:recognizer.view];
        CGFloat xDist = (a.x - b.x);
        CGFloat yDist = (a.y - b.y);
        CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist));

        CGFloat scale = 1;
        if (old_distance != 0) {
            scale = distance / old_distance;
        }
        old_distance = distance;
        recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, scale, scale);
    }
}