【问题标题】:UIImageView and UIScrollView zoomingUIImageView 和 UIScrollView 缩放
【发布时间】:2012-02-12 01:32:03
【问题描述】:

我真的需要在 UIScrollView 中使用 UIPinchGestureRecognizer 才能使捏合工作吗?如果是,我该怎么做?我正在尝试实现翻转板所具有的功能,它基本上可以放大图像并在放大后具有滚动功能。我该怎么做?

更新:

这是我的一些代码,它不调用滚动视图委托

CGRect imgFrame;
imgFrame.size.width = originalImageSize.width;
imgFrame.size.height = originalImageSize.height;
imgFrame.origin.x = imageOriginPoint.x;
imgFrame.origin.y = imageOriginPoint.y;

NSData *data = [request responseData];
UIImage * image = [UIImage imageWithData:data];
imageView = [[UIImageView alloc] initWithImage:image];
[imageView setUserInteractionEnabled:YES];
[imageView setBackgroundColor:[UIColor clearColor]];
[imageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[imageView setFrame:CGRectMake(0, 0, imgFrame.size.width, imgFrame.size.height)];

UIScrollView * imgScrollView = [[UIScrollView alloc] initWithFrame:imageView.frame];
imgScrollView.delegate = self;
imgScrollView.showsVerticalScrollIndicator = NO;
imgScrollView.showsHorizontalScrollIndicator = NO;
[imgScrollView setScrollEnabled:YES];
[imgScrollView setClipsToBounds:YES];
[imgScrollView addSubview:imageView];
[imgScrollView setBackgroundColor:[UIColor blueColor]];
[imgScrollView setMaximumZoomScale:1.0];

【问题讨论】:

    标签: iphone objective-c ios ipad uiscrollview


    【解决方案1】:

    目标 C

    滚动视图步骤中的缩放图像(目标 C)

    1.滚动视图约束

    2。在ScrollView中添加ImageView并设置约束

    3.参加 IBOutlets

    IBOutlet UIScrollView * bgScrollView;
    IBOutlet UIImageView * imageViewOutlet;
    

    4. viewDidLoad 方法

    - (void)viewDidLoad
     {
        [super viewDidLoad];
    
        float minScale=bgScrollView.frame.size.width / imageViewOutlet.frame.size.width;
        bgScrollView.minimumZoomScale = minScale;
        bgScrollView.maximumZoomScale = 3.0;
        bgScrollView.contentSize = imageViewOutlet.frame.size;
        bgScrollView.delegate = self;
    }
    

    5.滚动视图委托方法

    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
    {
        return imageViewOutlet;
    }
    

    斯威夫特

    滚动视图步骤中的捏合缩放图像 (Swift)

    1.滚动视图约束

    2。在ScrollView中添加ImageView并设置约束

    3.参加 IBOutlets

    @IBOutlet weak var bgScrollView : UIScrollView!
    @IBOutlet weak var imageView : UIImageView!
    

    4. viewDidLoad 方法

     override func viewDidLoad() {
        super.viewDidLoad()
    
        let minScale = bgScrollView.frame.size.width / imageView.frame.size.width;
        bgScrollView.minimumZoomScale = minScale
        bgScrollView.maximumZoomScale = 3.0
        bgScrollView.contentSize = imageView.frame.size
        bgScrollView.delegate = self
    }
    

    5.滚动视图委托方法

    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return imageView
    }
    

    【讨论】:

      【解决方案2】:

      对于 'snap-image-to-screen-edges' 解决方案,@Shrikant Tanwade 效果很好,但我想要一些插图。我尝试了设置约束的常量,但这不起作用。

      我以以下解决方案结束:

      1. 与 Shrikant 的相同 - 设置 scrollViewimageView 的约束,将它们的常量设置为 0

      2. scrollView.contentInset = UIEdgeInsetsMake(....设置插入

      【讨论】:

        【解决方案3】:

        您需要做的就是在您的UIScrollView 中添加您的UIImageView(或您想要缩放的任何视图)。

        UIScrollView 上的maximumZoomScale 设置为高于1.0f 的任何值。

        将自己设置为 UIScrollView 的委托,并在 viewForZooming 委托方法中返回 UIImageView

        就是这样。不需要捏手势,什么都没有。 UIScrollView 为您处理捏缩放。

        【讨论】:

        • 是的,我做了所有这些,但没有调用代理
        • 啊哈!我没有设置 maxZoomScale,因此它不起作用!谢谢
        • Ignacio,你能告诉我对 imageView 和 scrollView 的限制吗?我做了同样的事情,但它不工作。我的图像视图设置为宽高比填充,宽度超出图像视图框架,因此我需要增加图像视图框架以及滚动视图内容大小宽度。我的滚动视图中只有一张图片
        • 如果您需要缩小功能,只需设置 minimumZoomScale 属性。
        【解决方案4】:

        我想这个答案可能是不必要的,但我遇到了与 @adit 类似的问题,并以一种非常简单的方式解决了它(我认为),也许有类似挑战的人可以使用它:

        - (void)viewDidLoad {
            [super viewDidLoad];
        
            self.view.backgroundColor = [UIColor yellowColor];
        
            UIImage *imageToLoad = [UIImage imageNamed:@"background_green"];
        
            self.myImageView = [[UIImageView alloc]initWithImage:imageToLoad];
            self.myScrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
            [self.myScrollView addSubview:self.myImageView];
            self.myScrollView.contentSize = self.myImageView.bounds.size;
            self.myScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
            self.myScrollView.minimumZoomScale = 0.3f;
            self.myScrollView.maximumZoomScale = 3.0f;
            self.myScrollView.delegate = self;
            [self.view addSubview:self.myScrollView];
        }
        
        - (UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView {
            NSLog(@"viewForZoomingInScrollView");
            return self.myImageView;
        } 
        

        我的问题很简单,我忘记添加self.myScrollView.delegate = self;,这就是我遇到问题的原因。我花了很长时间才弄清楚这个简单的问题,我想我没有看到所有树木的福雷斯特 :-)

        【讨论】:

          【解决方案5】:

          不久前我做了一个自定义图像查看器,没有捏识别器。只是 UIScrollView 之上的 UIImageView。在那里你传递了一个带有图像链接的字符串,它还有一个进度条。一旦该图像完成加载,就会显示该图像。代码如下:

          -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
          {
            return self.theImageView;
          }
          
          - (CGRect)centeredFrameForScrollView:(UIScrollView *)scroll andUIView:(UIView *)rView {
            CGSize boundsSize = scroll.bounds.size;
            CGRect frameToCenter = rView.frame;
            // center horizontally
            if (frameToCenter.size.width < boundsSize.width) {
              frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
            }
            else {
              frameToCenter.origin.x = 0;
            }
            // center vertically
            if (frameToCenter.size.height < boundsSize.height) {
              frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
            }
            else {
              frameToCenter.origin.y = 0;
            }
            return frameToCenter;
          }
          
          -(void)scrollViewDidZoom:(UIScrollView *)scrollView
          {
            self.theImageView.frame = [self centeredFrameForScrollView:self.theScrollView andUIView:self.theImageView];                               
          }
          
          - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
            [self.resourceData setLength:0];
            self.filesize = [NSNumber numberWithLongLong:[response expectedContentLength]];
          }
          
          -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
          {
            [self.resourceData appendData:data];
            NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[self.resourceData length]];
            self.progressBar.progress = [resourceLength floatValue] / [self.filesize floatValue];
          }
          
          -(void)connectionDidFinishLoading:(NSURLConnection *)connection
          {
            self.theImage = [[UIImage alloc]initWithData:resourceData];
            self.theImageView.frame = CGRectMake(0, 0, self.theImage.size.width, self.theImage.size.height);
            self.theImageView.image = self.theImage;
            self.theScrollView.minimumZoomScale = self.theScrollView.frame.size.width / self.theImageView.frame.size.width;
            self.theScrollView.maximumZoomScale = 2.0;
            [self.theScrollView setZoomScale:self.theScrollView.minimumZoomScale];
            self.theScrollView.contentSize = self.theImageView.frame.size;
            self.theLabel.hidden = YES;
            self.progressBar.hidden = YES;
          }
          
          -(void)setImageInImageView
          {
            NSURLRequest *req = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:self.imageLink]];
            NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:req delegate:self];
            if (conn)
            {
              self.resourceData = [NSMutableData data];
            }
            else
            {
              NSLog(@"Connection failed: IMageViewerViewController");
            }
          }
          
          -(void)loadView
          {
            self.filesize = [[NSNumber alloc]init];
            self.progressBar = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar];
            self.progressBar.frame = CGRectMake(20, 240, 280, 40);
            [self.progressBar setProgress:0.0];
            self.theImageView = [[[UIImageView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]]autorelease];
            self.theScrollView = [[[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]]autorelease];
            self.theScrollView.delegate = self;
            [self.theScrollView addSubview:self.theImageView];
            self.view = self.theScrollView;
            self.theLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 200, 320, 40)];
            self.theLabel.font = [UIFont boldSystemFontOfSize:15.0f];
            self.theLabel.text = @"Please wait, file is being downloaded";
            self.theLabel.textAlignment = UITextAlignmentCenter;
            self.theLabel.hidden = NO;
            [self.view addSubview:self.progressBar];
            [self.view bringSubviewToFront:self.progressBar];
            [self.view addSubview:self.theLabel];
            [self.view bringSubviewToFront:self.    theLabel];
            [self performSelectorOnMainThread:@selector(setImageInImageView) withObject:nil waitUntilDone:NO];
          }
          

          还有头文件:

          @interface ImageViewerViewController : UIViewController<UIScrollViewDelegate, NSURLConnectionDelegate, NSURLConnectionDataDelegate>
          
          @property (nonatomic, retain) IBOutlet UIImageView *theImageView;
          @property (nonatomic, retain) IBOutlet UIScrollView *theScrollView;
          @property (nonatomic, retain) NSString *imageLink;
          @property (nonatomic, retain) UIImage *theImage;
          @property (nonatomic, retain) UILabel *theLabel;
          @property (nonatomic, retain) UIProgressView *progressBar;
          @property (nonatomic, retain) NSMutableData *resourceData;
          @property (nonatomic, retain) NSNumber *filesize;
          @end
          

          希望对你有帮助

          【讨论】:

          • 你知道为什么永远不会调用 viewForZoomingInScrollView 吗?
          • @adit 我相信它是由 UIScrollViewDelegate 调用的
          • 从外观上看,这基本上是 zoomingPDFViwer 的改良版,更好。我对@Novarg 有一个问题。当我加载 PDF 时,为什么它会从 tableview 下载到滚动视图,但在第一次选择它时不会更新滚动视图,但第二次就可以了,这是为什么?
          • @mattInman 它可能与 zoomingPDFViewer 几乎相同,我只是收集了很多代码,然后使用这些代码中的片段来创建我想要的类。对不起,我不明白你的问题。你能更具体一点吗?而且这个类是用来显示图像(jpg/jpeg/gif/bmp/png/etc.),所以我不确定它是否适用于 PDF。
          • @Novarg 没关系,我找到了答案。
          【解决方案6】:

          当我在 UIScrollView 中开发 Zooming PDF 查看器时,我发现在手机上运行它时,它实际上已经将缩放功能作为手机的一部分实现了。但是你可以看看这个,http://developer.apple.com/library/ios/#samplecode/ZoomingPDFViewer/Introduction/Intro.html 这是一段尝试实现缩放功能的示例代码,这是我的开始。

          【讨论】:

          • 我实际上看过它,它说我需要一个名为 viewForZoomingInScrollView 的委托,但是尽管我已经将委托设置为 self,但它从未被调用过
          • 就像@Novarg 说它被 UIScrollViewDelegate 调用所以在你的 .h 文件中确保添加
          猜你喜欢
          • 2017-09-23
          • 2012-08-16
          • 2012-11-24
          • 1970-01-01
          • 2019-05-23
          • 2018-08-24
          • 2014-01-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多