【问题标题】:Zooming UIImageView inside UIScrollView with autolayout使用自动布局在 UIScrollView 内缩放 UIImageView
【发布时间】:2013-01-06 10:27:08
【问题描述】:

我在 UIScrollView 中嵌入了一个 UIImageView,在 iOS 6 和自动布局之前,我在控制器的 viedDidLoad 方法中使用了以下 sn-p 来显示可滚动和可缩放的图像。

self.scrollView.contentSize = self.imageView.image.size;
self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);

但现在使用情节提要中设置的约束。我在 SO 中发现了这个问题Embed ImageView in ScrollView with Auto Layout on iOS 6 和其他一些问题,指出约束是在 viewDidLoad 之后加载/强制执行的,并且将我以前的 sn-p 移动到 viewDidAppear 可以解决这个问题,但是缩放不能正常工作,而且似乎scrollView 和 imageView 的大小在捏合缩放手势后重置为情节提要的约束。

我只是在猜测,但我认为如果有某种方法可以覆盖代码中的 scrollView 和 imageView 的垂直和水平空间约束,可能会起作用。

还有其他人有这个问题吗?

【问题讨论】:

    标签: ios6 uiscrollview uiimageview autolayout


    【解决方案1】:

    【讨论】:

    • 这样更好,但是当我缩小我的图像时,我的图像位于左上角而不是滚动视图的中心(或者不是图像的角位于原点)。另请注意,该示例有一个 IBOutletCollection 约束,该约束必须包含 UIImageView 上的所有约束,否则在缩放时会出现异常。
    • 我通过关闭 translatesAutoresizingMaskIntoConstraints 解决了我的问题,然后在 viewWillLayoutSubviews 中将框架的原点设置为 0,0。图像没有居中,但至少在缩放时不会滑出可滚动区域。
    • 我用过 [_imgProduct setCenter:_scrProduct.center];而不是 0,0 和 translatesAutoresizingMaskIntoConstraints = NO;
    • github.com/evgenyneu/ios-imagescroll 看看这个。非常适合我。
    【解决方案2】:

    zsolt 在 cmets 中提出了最好的解决方案:

    http://github.com/evgenyneu/ios-imagescroll 看看这个。非常适合我。

    此存储库中提出的解决方案是在显示图像之前调整最小和当前缩放级别:

    @interface MyViewController () <UIScrollViewDelegate>
    @property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
    @property (weak, nonatomic) IBOutlet UIImageView *imageView;
    @end
    
    
    @implementation MyViewController
    
    - (void)viewDidLoad
    {
      [super viewDidLoad];
      self.scrollView.delegate = self;
    
      [self initZoom];
    }
    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    {
      [self initZoom];
    }
    
    // Zoom to show as much image as possible
    - (void) initZoom {
      float minZoom = MIN(self.view.bounds.size.width / self.imageView.image.size.width,
                          self.view.bounds.size.height / self.imageView.image.size.height);
      if (minZoom > 1) return;
    
      self.scrollView.minimumZoomScale = minZoom;
    
      self.scrollView.zoomScale = minZoom;
    }
    
    - (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
    {
      return self.imageView;
    }
    

    但是,缩小样本时会出现问题。图像未居中。这可以通过使用包含以下代码的自定义滚动视图类轻松解决:

    @interface MyScrollView : UIScrollView
    @end
    
    @implementation MyScrollView
    
    -(void)layoutSubviews
    {
      [super layoutSubviews];
      UIView* v = [self.delegate viewForZoomingInScrollView:self];
      CGFloat svw = self.bounds.size.width;
      CGFloat svh = self.bounds.size.height;
      CGFloat vw = v.frame.size.width;
      CGFloat vh = v.frame.size.height;
      CGRect f = v.frame;
      if (vw < svw)
        f.origin.x = (svw - vw) / 2.0;
      else
        f.origin.x = 0;
      if (vh < svh)
        f.origin.y = (svh - vh) / 2.0;
      else
        f.origin.y = 0;
      v.frame = f;
    }
    
    @end
    

    【讨论】:

    【解决方案3】:

    我也同意Zsolt的建议和链接。

    但我更新了宽度/高度约束以允许它处理任何尺寸的图像:

    - (void) initZoom
    {
      for (NSLayoutConstraint *constraint in self.photoImageView.constraints)
      {
        if (constraint.firstAttribute == NSLayoutAttributeWidth)
          constraint.constant = self.photoImageView.image.size.width;
        else if (constraint.firstAttribute == NSLayoutAttributeHeight)
          constraint.constant = self.photoImageView.image.size.height;
      }
    
      float minZoom = MIN(self.scrollView.bounds.size.width / self.photoImageView.image.size.width,
                          self.scrollView.bounds.size.height / self.photoImageView.image.size.height);
      if (minZoom > 1) return;
    
      self.scrollView.minimumZoomScale = minZoom;
    
      self.scrollView.zoomScale = minZoom;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-11-07
      • 2012-12-11
      • 2014-11-27
      • 1970-01-01
      • 1970-01-01
      • 2013-01-04
      • 1970-01-01
      • 1970-01-01
      • 2014-01-15
      相关资源
      最近更新 更多