是否可以在UIScrollView中放大和缩小UIImageView,但坚持使用自动布局?

时间:2014-02-12 03:53:09

标签: ios objective-c uiscrollview uiimageview autolayout

长话短说,我正在尝试构建类似于Photos.app的功能。

我有一个UIScrollView,里面有一个UIImageView,它在Storyboard中设置。缩放工作,但我无法保持中心。在我所有基于帧的滚动视图实现中,我将它集中在如下,它的工作非常好:

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    CGRect newImageViewFrame = self.imageView.frame;

    // Center horizontally
    if (newImageViewFrame.size.width < CGRectGetWidth(scrollView.bounds)) {
        newImageViewFrame.origin.x = (CGRectGetWidth(scrollView.bounds) - CGRectGetWidth(self.imageView.frame)) / 2;
    }
    else {
        newImageViewFrame.origin.x = 0;
    }

    // Center vertically
    if (newImageViewFrame.size.height < CGRectGetHeight(scrollView.bounds)) {
        newImageViewFrame.origin.y = (CGRectGetHeight(scrollView.bounds) - CGRectGetHeight(self.imageView.frame)) / 2;
    }
    else {
        newImageViewFrame.origin.y = 0;
    }

    self.imageView.frame = newImageViewFrame;
}

但是使用自动布局它根本就不存在。

我对UIScrollView或UIImageView没有任何限制,因为我不知道它们应该是什么。我想我应该将UIScrollView粘贴到四个角落,但对于UIImageView我不完全确定,因为缩放会改变它的框架。

以下是一个示例项目:http://cl.ly/21371H3q381N

如何使用自动布局进行缩放工作?

2 个答案:

答案 0 :(得分:1)

使用Autolayout时,对setFrames的调用没有生效,这就是为什么imageView不在scrollView中居中。

话虽如此,为了达到中心效果,您可以选择:

  1. 最简单的方法是为translatesAutoresizingMaskIntoConstraints中的imageView设置YESViewDidLoad,这会将对setFrame:的调用转换为基于您的新约束imageView autoresizingMask。但是你应该确保你在故事板中设置的新约束满足(在你的情况下没有)。

  2. scrollViewDidZoom:方法中直接添加约束以使您的imageView居中

  3. -

     [self.imageView addConstraint:[NSLayoutConstraint constraintWithItem:self.imageView
           attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual
           toItem:self.scrollView attribute:NSLayoutAttributeCenterX multiplier:1.0
           constant:0]];
    
     [self.imageView addConstraint:[NSLayoutConstraint constraintWithItem:self.imageView
           attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual
           toItem:self.scrollView attribute:NSLayoutAttributeCenterX multiplier:1.0 
           constant:0]];
    

答案 1 :(得分:0)

要使内容居中,您可以参考以下代码段:

// To re-center the image after zooming in and zooming out
- (void)centerScrollViewContents
{
 CGSize boundsSize = self.bounds.size;
 CGRect contentsFrame = self.imageView.frame;

 if (contentsFrame.size.width < boundsSize.width)
 {
   contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
 }
 else
 {
  contentsFrame.origin.x = 0.0f;
 }

 if (contentsFrame.size.height < boundsSize.height)
 {
  contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
 }
 else
 {
    contentsFrame.origin.y = 0.0f;
 }

 self.imageView.frame = contentsFrame;
}

//for zooming in and zooming out
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
 {
  if (self.zoomScale > 1.0f)
  {
    [self centerScrollViewContents];
  }
   else
  {
  self.bouncesZoom    =   NO;

    // for zooming out by pinching
  [self centerScrollViewContents];
  }

 }

这样,您可以在放大和缩小后重新定位图像的坐标,而不使用AutoLayout。如果有帮助,请告诉我。谢谢:))