如何使用或不使用 ScrollView 使 ImageView 可缩放?

how to make an ImageView zoomable with or without ScrollView.?(如何使用或不使用 ScrollView 使 ImageView 可缩放?)
本文介绍了如何使用或不使用 ScrollView 使 ImageView 可缩放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我的 IB 中有一个 UImageView,并在该 UImageView 上添加了一个地图图像.我想让该图像可以缩放.

I have an UImageView in my IB and added a map image on that UImageView. I want to make that image pinch zoomable.

这是我的代码:

- (void)viewDidLoad
{
   [super viewDidLoad];
    self.title = @"Map";

   self.mapImageView.contentMode = UIViewContentModeScaleAspectFit;
   [self.mapScrollView addSubview:self.mapImageView];
   [self.mapScrollView setContentSize:CGSizeMake(self.mapImageView.frame.size.width, self.mapImageView.frame.size.height)];
   [self.mapScrollView setMinimumZoomScale:1.0];
   [self.mapScrollView setMaximumZoomScale:4.0];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
   return self.mapImageView;
}

但这无济于事,我可以看到 UIScrollView 来了,但我的图像没有任何反应.
提前致谢.

but this doesn't help, I can see a UIScrollView Coming but nothing happen to my image.
Thanks in advance.

推荐答案

在你的 ViewController.h 文件中添加 UIScrollViewDelegate

Add UIScrollViewDelegate in your ViewController.h file

然后将以下代码添加到您的 ViewController.m 文件中

then add following code to your ViewController.m file

如果您使用此代码,则无需添加 UIPinchGestureRecognizer

No need to add UIPinchGestureRecognizer if your using this code

    - (void)scrollViewDidZoom:(UIScrollView *)scrollView
    {

    UIView* zoomView = [scrollView.delegate viewForZoomingInScrollView:scrollView];

    CGRect zoomViewFrame = zoomView.frame;

    if(zoomViewFrame.size.width < scrollView.bounds.size.width)

    {

    zoomViewFrame.origin.x = (scrollView.bounds.size.width - zoomViewFrame.size.width) / 2.0;

    }

    else

    {
    zoomViewFrame.origin.x = 0.0;
    }

    if(zoomViewFrame.size.height < scrollView.bounds.size.height)

     {      zoomViewFrame.origin.y = (scrollView.bounds.size.height - zoomViewFrame.size.height) / 2.0;

    }
    else

    {
    zoomViewFrame.origin.y = 0.0;
    }
    zoomView.frame = zoomViewFrame;
    }

修改viewDidLoad如下

- (void)viewDidLoad
 {
     [super viewDidLoad];
    self.mapScrollView.delegate = self;
    self.mapScrollView.minimumZoomScale = 1.0;
    self.mapScrollView.maximumZoomScale = 4.0;
    UIImage * myImage= [UIImage imageNamed:@"Background.png"]; //add your image here
    [self.mapImageView setImage:myImage];
    [self.mapImageView sizeToFit];
    self.mapScrollView.contentSize = myImage.size;

}

在此处指定要放大的imageView

Specify the imageView to zoom here

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.mapImageView;
}

要避免 UIScrollView 在 zoomimg 上的奇怪行为,请使用此方法

To avoid the strange behaviour of UIScrollView on zoomimg use this method

- (void)view:(UIView*)view setCenter:(CGPoint)centerPoint
{
    CGRect viewFrame = view.frame;
        CGPoint contentOffset = self.mapScrollView.contentOffset;

        CGFloat x = centerPoint.x - viewFrame .size.width / 2.0;
        CGFloat y = centerPoint.y - viewFrame .size.height / 2.0;

    if(x < 0)
    {
        contentOffset.x = -x;
        viewFrame .origin.x = 0.0;
    }
    else
    {
        viewFrame .origin.x = x;
    }
    if(y < 0)
    {
        contentOffset.y = -y;
        viewFrame .origin.y = 0.0;
    }
    else
    {
        viewFrame .origin.y = y;
    }

    view.frame = viewFrame ;
    self.mapScrollView.contentOffset = contentOffset;
}

然后在viewDidAppear上调用上面的方法

Then call the above method on viewDidAppear

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    CGPoint centerPoint = CGPointMake(CGRectGetMidX(self.mapScrollView.bounds),
                                  CGRectGetMidY(self.mapScrollView.bounds));
    [self view:self.mapImageView setCenter:centerPoint];
}

看看这个 链接.我从那个链接学到了这项技术.

have a look at this link if you need more clarification. I learned this technique from that link.

这篇关于如何使用或不使用 ScrollView 使 ImageView 可缩放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Stop a UITableView from automatically scrolling(阻止 UITableView 自动滚动)
iOS UIScrollView Lazy Loading(iOS UIScrollView 延迟加载)
using iOS 6.0 SDK and building for iOS 5 Target causes UIScrollView setMinimumZoomScale to fail when running on iOS 5 simulator(在 iOS 5 模拟器上运行时,使用 iOS 6.0 SDK 并为 iOS 5 Target 构建会导致 UIScrollView setMinimumZoomScale 失败) -
Create partial-screen UIPageViewController programmatically(以编程方式创建部分屏幕 UIPageViewController)
UIImageView zoom and pinch in UIScrollView(UIImageView 在 UIScrollView 中缩放和捏合)
How can i add more than 10 buttons on a navigationbar in iphone application development?(如何在 iphone 应用程序开发中的导航栏上添加 10 多个按钮?)