首页 文章

如何在iOS中的imageview中添加手势识别器?

提问于
浏览
0

我在我的imageview中添加了一个UIPanGestureRecognizer,然后添加了Gesture Recognizer,但是我的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图像,但我希望图像仅在imageview内部进行平移,如果有可能则给我解决方案 . 这里我的原始imageview大小就像
enter image description here

当我添加UIPanGEstureRecognizer时,它看起来像

enter image description here
图像在视图中平移但我想在imageview大小内放大它请给我解决方案 .

3 回答

  • 0

    UIScrollView 并在scrollview中添加 UIImageView 并在scrollview上添加pangest ..

    为scrollview设置委托并执行代码

    - (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];
    }
    
  • 1

    UIImage对象's userInteractEnable is NO by default, so you can'与它交互 . 试试 self.zoomImage.userInteractEnable = YES

  • 0

    试试这个:
    imageView 下添加另一个 UIView ,使其大小与 imageView 完全相同 . 然后将 imageView 设置为此 UIView 的子视图,将 imageView 拖放到此 UIView 之上(假设您使用的是 StoryBoardxib ) .
    之后,如果您使用 StoryBoardxib ,则转到属性检查器,选择此 UIView ,勾选 Click Subviews . 如果没有,只需设置 view.clipsToBounds = YES;

相关问题