【问题标题】:Pinch zoom not working in UIScrollview for Multiple image in ios7在 ios7 中的多个图像的 UIScrollview 中捏缩放不起作用
【发布时间】:2013-12-05 05:55:49
【问题描述】:

我想捏缩放 uiscrollview 图像。我在 uiscrollview 中添加了多个图像 但捏缩放不起作用我想捏缩放 UIImageview 图像。我参考了很多参考资料,但仍然无法正常工作

提前致谢

我的代码:-

for(int i = 0;i<aryImage.count;i++)
            {

                 //Create a uiimageview

                imageView =[[UIImageView alloc]init ];

                imageView.frame = CGRectMake((280 *i),0.0, 280, 475);
                imageView.image = [UIImage imageNamed:@"default.png"];
                imageView.tag = i;
                imageView.userInteractionEnabled = YES;
                [scrollGallery addSubview:imageView];

                scrollGallery.minimumZoomScale = 1.0;

                scrollGallery.maximumZoomScale = 2.0;

                scrollGallery.delegate = self;

               [scrollGallery addSubview:imageView];

            }
            scrollGallery.contentSize = CGSizeMake(280 * aryImage.count, scrollGallery.frame.size.height);
            pageControl.numberOfPages = aryImage.count;
            pageControlBeingUsed = NO;
            pageControl.currentPage = 0;




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

【问题讨论】:

    标签: iphone xcode uiscrollview ios7


    【解决方案1】:

    试试这个:

    - (void)scrollViewDidZoom:(UIScrollView *)sv
    {
        UIView* zoomView = [sv.delegate viewForZoomingInScrollView:sv];
        CGRect zvf = zoomView.frame;
        if(zvf.size.width < sv.bounds.size.width)
        {
            zvf.origin.x = (sv.bounds.size.width - zvf.size.width) / 2.0;
        }
        else
        {
            zvf.origin.x = 0.0;
        }
        if(zvf.size.height < sv.bounds.size.height)
        {
            zvf.origin.y = (sv.bounds.size.height - zvf.size.height) / 2.0;
        }
        else
        {
            zvf.origin.y = 0.0;
        }
        zoomView.frame = zvf;
    }
    

    【讨论】:

      【解决方案2】:

      您在 viewForZooming 中仅返回一个 imageView,但您添加了一组它们。这可能是您的问题的原因。尝试像这样替换您的代码:

      UIView *containerView = [[UIView alloc] initWithFrame:scrollGallery.frame];
      [scrollGallery addSubview:containerView];
      scrollGallery.minimumZoomScale = 1.0;
      scrollGallery.maximumZoomScale = 2.0;
      scrollGallery.delegate = self;
      
      for(int i = 0;i<aryImage.count;i++)
              {
      
                   //Create a uiimageview
      
                  imageView =[[UIImageView alloc]init ];
      
                  imageView.frame = CGRectMake((280 *i),0.0, 280, 475);
                  imageView.image = [UIImage imageNamed:@"default.png"];
                  imageView.tag = i;
                  imageView.userInteractionEnabled = YES;
                  [containerView addSubview:imageView];
              }
              scrollGallery.contentSize = CGSizeMake(280 * aryImage.count, scrollGallery.frame.size.height);
      
              // then maybe you need to resize containerView's frame
              //..........
      
              pageControl.numberOfPages = aryImage.count;
              pageControlBeingUsed = NO;
              pageControl.currentPage = 0;
      
      
      - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
          return containerView;
      }
      

      希望它会有所帮助。

      UPD:也尝试禁用你的 imageViews 的 userInteraction。它们可能会阻止与 scrollGallery 的交互。

      UPD2:将所有 imageViews 存储在预定义的数组中。例如,

      NSMutableArray *arr = [[NSMutableArray alloc] init];
      for (int i=0; i<aryImage.count; i++) {
          [arr addObject:[[UIImageView alloc] init]];
      }
      

      然后在你的循环中而不是

      imageView =[[UIImageView alloc] init];
      

      使用

      UIImageView *imageView = [arr objectAtIndex:i];
      

      并且不要忘记在最后释放所有内容;)

      【讨论】:

      • 我使用 yr 代码,它适用于我,但是当我缩放第三张图像时它不会缩放它只缩放第一张图像:)
      • 您确定要将第三张图片添加到容器视图而不是滚动视图中吗?
      • @hatfinch 是的,我在缩放第三张图像时将所有图像添加到容器视图中,然后它只返回第一张图像:)
      • imageView 对象可能会在循环中释放,因为您在下一次迭代中将其替换为另一个对象。请参见上面的 UPD2。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-26
      • 2012-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多