2

我有一个 UIViewController 正在调用 UIView 类,并且我正在使用 CGAffine Transform 使用以下代码放大我的 UIView 类实例

CGAffineTransform transform = CGAffineTransformMakeScale(recognizer.scale,    recognizer.scale);
NSLog(@"Pinch scale: %f", recognizer.scale);
float scale = recognizer.scale;
float SCALE_MIN = 1.0f;
float SCALE_MAX = 3.0f;
. . . 

这个基本代码,有效!(部分代码已省略)

现在的问题是无论我在屏幕上的哪个位置“捏”文件总是从文件的中间缩放,而不是在我捏的位置。.

关于如何让它放大到被夹住的确切位置有什么建议吗?

4

3 回答 3

1

locationInView:方法在 all 上可用UIGestureRecognizers,当在 的实例上调用时UIPinchGestureRecognizer,将为您提供构成夹点的两次触摸之间的中点。这是我认为你最接近“被挤压的确切位置”的东西。

首次识别手势时,保存locationInView以获取要缩放的点。我不会尝试在捏合过程中更新缩放中心,因为我认为您最终会得到一个不直观的复杂实现。您将希望缩放保持该中心点固定,并且您应该能够通过设置您正在转换的视图的 来做到这anchorPoint一点layer。请注意,anchorPoint坐标是标准化的——默认是(0.5, 0.5),视图的中心。

于 2013-07-03T22:14:14.443 回答
0
- (void)pinchDetected:(UIPinchGestureRecognizer *)gestureRecognizer {

if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
    // Reset the last scale, necessary if there are multiple objects with different scales
    lastScale = [gestureRecognizer scale];
}

if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
    [gestureRecognizer state] == UIGestureRecognizerStateChanged) {

    CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];

    // Constants to adjust the max/min values of zoom
    const CGFloat kMaxScale = 2.2;
    const CGFloat kMinScale = 0.64;

    CGFloat newScale = 1 -  (lastScale - [gestureRecognizer scale]);
    newScale = MIN(newScale, kMaxScale / currentScale);
    newScale = MAX(newScale, kMinScale / currentScale);
    CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
    [gestureRecognizer view].transform = transform;

    [gestureRecognizer setScale:1.0];

    lastScale = [gestureRecognizer scale];  // Store the previous scale factor for the next pinch gesture call
}
}
于 2015-02-18T07:51:17.300 回答
-1

希望这对你有帮助它对我来说就像一个魅力......

- (void)pinchDetected:(UIPinchGestureRecognizer *)pinchRecognizer
{
    CGAffineTransform transform = CGAffineTransformScale(pinchRecognizer.view.transform, pinchRecognizer.scale,pinchRecognizer.scale);
    _designImage.transform = transform;
    _Zoomscale = pinchRecognizer.scale;

    NSLog(@"image Pinch scale: %2ff", _Zoomscale);
}
于 2014-07-11T11:12:37.653 回答