ios - 如何在 iOS 的 ImageView 中添加手势识别器?

标签 ios objective-c uipangesturerecognizer

我将 UIPanGestureRecognizer 添加到我的 imageview,然后添加了手势识别器,但我的 imageview 图像被平移到我的 View 中,我只想在 imageview 内部平移图像,这怎么可能?

我在 viewdidload 上写了一个代码

 UIPanGestureRecognizer *pangesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureDetected:)];
[pangesture setDelegate:self];
[self.zoomImage addGestureRecognizer:pangesture];

方法是

- (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer
{
UIGestureRecognizerState state = [recognizer state];

if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
{
    CGPoint translation = [recognizer translationInView:recognizer.view];
    [recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)];
    [recognizer setTranslation:CGPointZero inView:recognizer.view];
}
}

然后 imageView 图像在我的 View 中平移,但我希望图像仅在 imageview 内部平移(如果可能)然后给我解决方案。 这里我原来的 imageview 大小就像 enter image description here

当我添加 UIPanGEstureRecognizer 时,它看起来像
enter image description here 图像在 View 中平移,但我想将其缩放到 ImageView 大小内,请给我解决方案。

最佳答案

采用 UIScrollView 并在 ScrollView 内添加 UIImageView 并在 ScrollView 上添加 pangesture ..

为 ScrollView 设置委托(delegate)并执行代码

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    // Return the view that we want to zoom
    return self.zoomImage;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    // The scroll view has zoomed, so we need to re-center the contents
    [self centerScrollViewContents];
}

- (void)centerScrollViewContents {
    CGSize boundsSize = scrollView.bounds.size;
    CGRect contentsFrame = self.zoomImage.frame;

    if (contentsFrame.size.width < boundsSize.width) {
        contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
    } else {
        contentsFrame.origin.x = 0.0f;
    }

    if (contentsFrame.size.height < boundsSize.height) {
        contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
    } else {
        contentsFrame.origin.y = 0.0f;
    }

    self.zoomImage.frame = contentsFrame;
}

- (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer {
    // Get the location within the image view where we tapped
    CGPoint pointInView = [recognizer locationInView:imageView];

    // Get a zoom scale that's zoomed in slightly, capped at the maximum zoom scale specified by the scroll view
    CGFloat newZoomScale = scrollView.zoomScale * 1.5f;
    newZoomScale = MIN(newZoomScale, scrollView.maximumZoomScale);

    // Figure out the rect we want to zoom to, then zoom to it
    CGSize scrollViewSize = scrollView.bounds.size;

    CGFloat w = scrollViewSize.width / newZoomScale;
    CGFloat h = scrollViewSize.height / newZoomScale;
    CGFloat x = pointInView.x - (w / 2.0f);
    CGFloat y = pointInView.y - (h / 2.0f);

    CGRect rectToZoomTo = CGRectMake(x, y, w, h);

    [scrollView zoomToRect:rectToZoomTo animated:YES];
}

- (void)scrollViewTwoFingerTapped:(UITapGestureRecognizer*)recognizer {
    // Zoom out slightly, capping at the minimum zoom scale specified by the scroll view
    CGFloat newZoomScale = scrollView.zoomScale / 1.5f;
    newZoomScale = MAX(newZoomScale, scrollView.minimumZoomScale);
    [scrollView setZoomScale:newZoomScale animated:YES];
}

关于ios - 如何在 iOS 的 ImageView 中添加手势识别器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26857498/

相关文章:

ios - 如何将数据库存储在服务器中以便在 iOS 上访问?

ios - 在某些情况下使 superview 响应 subViews Pan Gesture

ios - 如何实现像 iOS MAIL 应用程序这样的平移手势,以便一次只打开一个单元格?

ios - 使用 PanGestureRecognizer 在与另一个 View 碰撞之前检测 View 的最后一个中心点?

html - WKWebView:加载本地html时如何将文本插入HTML

ios - 如何从 iphone 中的单个 viewcontroller 停止 tabbar 的三个 viewcontroller 中的计时器?

ios - totalBytesExpectedToWrite 在 NSURLSessionDownloadTask 中为 -1

iphone - 删除NSURL路径的内容

objective-c - iPad 中 LandscapeLeft 模式下的 UI 问题

ios - 在 Swift 中仅导入 ObjC header : No Such Module error