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

如何在iOS的imageview中添加手势识别器?,ios,objective-c,uipangesturerecognizer,Ios,Objective C,Uipangesturerecognizer,我在imageview中添加了一个UIPangestureRecognitor,然后添加了手势识别器,但我的imageview图像被平移到我的视图中我只想要图像被平移到imageview的内部在视图中如何可能 我在viewdidload上为此编写了一个代码 UIPanGestureRecognizer *pangesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureDetecte

我在imageview中添加了一个UIPangestureRecognitor,然后添加了手势识别器,但我的imageview图像被平移到我的视图中我只想要图像被平移到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大小如下

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

图像在视图中被平移,但我想将其放大到图像视图大小内。请给我解决方案。

UIImage对象的UserInteractiEnable默认为否,因此您无法与它交互。试试self.zoomImage.userinteractivatenable=YES试试这个:
imageView
下添加另一个
UIView
,使其大小与您的
imageView
完全相同。然后将
imageView
设置为
UIView
的子视图,方法是将
imageView
拖放到此
UIView
顶部(假设您使用的是
StoryBoard
xib

之后,如果您使用的是
情节提要
xib
,则转到属性检查器,选择此
ui视图
,勾选
单击子视图
。如果没有,只需设置
view.clipstobunds=YES

获取
UIScrollView
并将
UIImageView
添加到scrollview中,并在scrollview上使用PangTesture

为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];
}
-(UIView*)视图用于缩放CrollView:(UICrollView*)滚动视图{
//返回要缩放的视图
返回self.zoomImage;
}
-(无效)scrollViewDidZoom:(UIScrollView*)scrollView{
//滚动视图已缩放,因此我们需要将内容重新居中
[self-centerScrollViewContents];
}
-(无效)centerScrollViewContents{
CGSize boundsSize=scrollView.bounds.size;
CGRect contentsFrame=self.zoomImage.frame;
if(contentsFrame.size.width
为了更好地理解,请共享一些图像。为此,您必须在ImageView中添加ImageView!因为您只能在UIView的子类或对象中向view和UIImage的对象添加手势!或者,您可以将您的ImageView置于特定的CustomView中,然后在其上添加平移手势!我相信您希望看到这一点,将每个UIImageView放在不同的UIView中,您的问题就会得到解决。它已启用#mengxiangjian它正在工作,但我希望缩放图像,而不是在我的视图中,而是在我的imageview中。我建议您将UIImageView放在UIScrollView中,然后,您不必向ImamgeView添加平移手势,因为滚动可以做到这一点。#mengxiangjian我在侧边my scrollview中添加了我的图像视图,但只将我的数组最后一个图像添加到滚动视图中,而不是滚动视图中的所有图像。