使用多个图像缩放UIScrollView

时间:2010-03-04 15:54:35

标签: iphone cocoa-touch uikit

我有一个包含多个UIImageViews的UIScrollFrame。

支持缩放这种控件的正确方法是什么?

哪个视图应该'viewForZoomingInScrollView'返回?

3 个答案:

答案 0 :(得分:1)

我最后通过在scrollview中放置一个scrollview来实现它。 这样,内部视图处理页面滑动,而外部手柄缩放。

答案 1 :(得分:0)

Apple有一个非常好的示例代码,全部是UIScrollView和缩放和平铺。查看

http://developer.apple.com/iphone/library/samplecode/ScrollViewSuite/index.html

此代码在WWDC2009的Scoll Views会话中讨论。遗憾的是,该会话的视频不是免费提供的,但您可以将其作为iPhone WWDC2009视频包的一部分购买。

答案 2 :(得分:0)

只需将滚动视图放在另一个内。

就像那样:

    -(void)zoomToSelectedImage:(NSIndexPath *)indexPath
    {

      int currentPage = indexPath.row;

    //The External Scrollview. It move back and forward
      UIScrollView *contentViewZoom = [[UIScrollView alloc] initWithFrame:self.frame];
      [contentViewZoom setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.8]];
      [contentViewZoom setContentSize:CGSizeMake(contentViewZoom.frame.size.width * [arrayOfImages count], contentViewZoom.frame.size.height)];
      [contentViewZoom setPagingEnabled:YES];
      [contentViewZoom setDelegate:self];


      for (int i = 0; i < [arrayOfImages count]; i++) {
        CGRect frame;
        frame.origin.x = contentViewZoom.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = contentViewZoom.frame.size;

        //The Internal Scrollview. It allow you zoom the picture

        UIScrollView *imageScrollView = [[UIScrollView alloc]initWithFrame:frame];
        [imageScrollView setTag:1];
        [imageScrollView setMinimumZoomScale:1.0];
        [imageScrollView setMaximumZoomScale:3.0];
        [imageScrollView setAlpha:1];
        [imageScrollView setDelegate:self];
        [imageScrollView setBouncesZoom:YES];
        [imageScrollView setClipsToBounds:YES];
        [imageScrollView setShowsHorizontalScrollIndicator:NO];
        [imageScrollView setShowsVerticalScrollIndicator:NO];
        [imageScrollView setContentSize:CGSizeMake(768, 1024)];


        UIImage *image = [arrayOfImages objectAtIndex:i];
        UIImageView* imgView = [[UIImageView alloc] init];
        [imgView setImage:image];
        [imgView setFrame:CGRectMake(0, 0, 768, 1024)];
        imgView.bounds = self.bounds;
        [imgView setClipsToBounds:YES];
        [imgView setContentMode:UIViewContentModeScaleAspectFit];
        [imgView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

        [imageScrollView addSubview:imgView];
        [contentViewZoom addSubview:imageScrollView];

      }


      [self addSubview:contentViewZoom];  
      [contentViewZoom setAlpha:0.2];


      //Positioning according to current position
      CGRect frame;
      frame.origin.x = contentViewZoom.frame.size.width * currentPage;
      frame.origin.y = 0;
      frame.size = contentViewZoom.frame.size;
      [contentViewZoom scrollRectToVisible:frame animated:NO];

    }

OBS:在我的情况下,屏幕尺寸是固定的(768x1024),您应该根据需要进行更改。