久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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

how to make an ImageView zoomable with or without ScrollView.?(如何使用或不使用 ScrollView 使 ImageView 可縮放?)
本文介紹了如何使用或不使用 ScrollView 使 ImageView 可縮放?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我的 IB 中有一個(gè) UImageView,并在該 UImageView 上添加了一個(gè)地圖圖像.我想讓該圖像可以縮放.

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;
}

但這無濟(jì)于事,我可以看到 UIScrollView 來了,但我的圖像沒有任何反應(yīng).
提前致謝.

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 上的奇怪行為,請(qǐng)使用此方法

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上調(diào)用上面的方法

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];
}

看看這個(gè) 鏈接.我從那個(gè)鏈接學(xué)到了這項(xiàng)技術(shù).

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

這篇關(guān)于如何使用或不使用 ScrollView 使 ImageView 可縮放?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Stop a UITableView from automatically scrolling(阻止 UITableView 自動(dòng)滾動(dòng))
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 模擬器上運(yùn)行時(shí),使用 iOS 6.0 SDK 并為 iOS 5 Target 構(gòu)建會(huì)導(dǎo)致 UIScrollView setMinimumZ
Create partial-screen UIPageViewController programmatically(以編程方式創(chuàng)建部分屏幕 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 應(yīng)用程序開發(fā)中的導(dǎo)航欄上添加 10 多個(gè)按鈕?)
主站蜘蛛池模板: 免费看黄色小视频 | 一级黄色片在线看 | 888久久久| 黄色片在线免费看 | 欧美精品日韩精品国产精品 | 欧美激情久久久 | 欧美成人一区二区三区片免费 | 日韩午夜电影在线观看 | a在线免费观看视频 | 国产精品乱码一区二三区小蝌蚪 | 欧美综合在线观看 | 亚洲国产精品视频一区 | 精品久久香蕉国产线看观看亚洲 | 亚洲不卡在线观看 | 伊人狠狠操 | 91成人 | 天天影视网天天综合色在线播放 | 91精品久久久久久久久中文字幕 | 国产一区二区三区在线 | 中文字幕av第一页 | 欧美一区免费 | 免费小视频在线观看 | 在线看一区二区三区 | 欧美一页 | 91av视频在线免费观看 | 国产高清在线观看 | 午夜视频免费在线观看 | 嫩草视频网 | 精精国产xxxx视频在线播放 | 黄色综合| 精品欧美乱码久久久久久1区2区 | 欧美乱大交xxxxx另类电影 | 中文字幕成人在线 | 久久精品国产亚洲夜色av网站 | 久久久久久久一区 | 日韩在线一区二区 | 亚洲精品久久久久久下一站 | 日日av | 中文字幕久久精品 | 国产精品小视频在线观看 | 精品久久精品 |