【问题标题】:Pinch Gesture Zoom only Zooms UIImage from Upper Left Corner仅捏手势缩放从左上角缩放 UIImage
【发布时间】:2014-07-04 05:28:47
【问题描述】:

我对此相当陌生,我正在尝试使用捏合手势放大 UIImage 并能够放大图像的任何特定部分。但是,当我缩放时,它只会从 UIView 的左上角缩放。所以我只能看到放大的图像的左上角。我希望能够像照片应用程序的工作方式一样缩放/平移图像。到目前为止,这是我的代码:

在 ViewDidLoad 中:

...
 // Load the image to be viewed into the UIImage
self.theImage.image = self.theNewImage;

UIPinchGestureRecognizer *pinchGestRecog = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)];

// ADD GESTURE RECOGNIZER TO THE VIEW
[theImage addGestureRecognizer:pinchGestRecog];

// ALLOW USER INTERACTION ON THE VIEW
[theImage setUserInteractionEnabled:YES];

// SET IMAGE ZOOM SCALE LIMITS
imageCurrentScale = 1.0;
imageMaxScale = 2.0;
imageMinScale = 0.5;

然后在我的 twoFingerPinch 方法中:

 - (void)twoFingerPinch:(UIPinchGestureRecognizer *)aPinchGesture
{
if (imageCurrentScale * [aPinchGesture scale] > imageMinScale && imageCurrentScale * [aPinchGesture scale] < imageMaxScale) {

    imageCurrentScale = imageCurrentScale * [aPinchGesture scale];
    CGAffineTransform zoomTransform = CGAffineTransformMakeScale(imageCurrentScale, imageCurrentScale);
    [[aPinchGesture view] setTransform:zoomTransform];
}
[aPinchGesture setScale:1.0];

}

以某种方式平移是答案吗?我不太确定平移是如何工作的。 有什么建议?谢谢。

【问题讨论】:

    标签: ios7 uiview xcode5 pinchzoom uipinchgesturerecognizer


    【解决方案1】:

    之所以放大到左上角,是因为缩放变换的中心是UIView.layer.anchorPoint,默认为左上角(0,0)。 如果您想正确缩放,则必须以某种方式找出捏合手势的中心,然后将 anchorPoint 设置为该位置。

    但无论如何你都不应该这样做。放大UIImageView 的推荐方法是将图像视图放入UIScrollView 并相应地设置contentSizemaximumZoomScaleminimumZoomScale 值。 将自己设置为 UIScrollView 的委托,并在 viewForZooming 委托方法中返回 UIImageView

    滚动视图将为您处理捏合和缩放。

    【讨论】:

      猜你喜欢
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多