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

时间:2014-11-11 04:31:08

标签: ios objective-c uipangesturerecognizer

我在我的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 :(得分:1)

选择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 :(得分:0)

UIImage对象的userInteractEnable默认为NO,因此您无法与之交互。试试self.zoomImage.userInteractEnable = YES

答案 2 :(得分:0)

试试这个:
UIView下添加另一个imageView,使其大小与imageView完全相同。然后将imageView设置为此UIView的子视图,将imageView拖放到此UIView之上(假设您正在使用StoryBoard或{{ 1}})。
之后,如果您使用的是xibStoryBoard,请转到属性检查器,选择此xib,勾选UIView。如果没有,请设置Click Subviews