Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 是否可以在UIScrollView内部使用UIImageView进行放大和缩小,但使用自动布局将其保持在中心位置?_Ios_Objective C_Uiscrollview_Uiimageview_Autolayout - Fatal编程技术网

Ios 是否可以在UIScrollView内部使用UIImageView进行放大和缩小,但使用自动布局将其保持在中心位置?

Ios 是否可以在UIScrollView内部使用UIImageView进行放大和缩小,但使用自动布局将其保持在中心位置?,ios,objective-c,uiscrollview,uiimageview,autolayout,Ios,Objective C,Uiscrollview,Uiimageview,Autolayout,长话短说,我正在尝试构建类似Photos.app的功能 我有一个UIScrollView,里面有一个UIImageView,设置在情节提要中。缩放可以工作,但我很难保持它居中。在我所有基于帧的滚动视图实现中,我将其居中,如下所示,效果非常好: - (void)scrollViewDidZoom:(UIScrollView *)scrollView { CGRect newImageViewFrame = self.imageView.frame; // Center horiz

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

我有一个UIScrollView,里面有一个UIImageView,设置在情节提要中。缩放可以工作,但我很难保持它居中。在我所有基于帧的滚动视图实现中,我将其居中,如下所示,效果非常好:

- (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;
}
-(无效)scrollViewDidZoom:(UIScrollView*)scrollView{
CGRect newImageViewFrame=self.imageView.frame;
//水平居中
if(newImageViewFrame.size.width
但对于自动布局,它根本不是

我对UIScrollView或UIImageView没有任何限制,因为我不知道它们应该是什么。我想我应该把UIScrollView粘到四个角上,但是对于UIImageView,我不能完全确定,因为缩放改变了它的帧

下面是一个示例项目:


如何使用Auto Layout进行居中缩放?

使用Autolayout时,对setFrames的调用不起作用,这就是为什么imageView未在滚动视图中居中

也就是说,为了实现中心效果,您可以选择:

  • 最简单的方法是在
    viewdiload
    中将图像视图的
    translatesAutoResizengMaskintoConstraints
    设置为
    YES
    ,这将根据图像视图将对
    setFrame:
    的调用转换为新的约束。但是,您应该确保新的约束与您在故事板中设置的约束相匹配(在您的案例中,没有)

  • scrollViewDidZoom:
    方法中,直接添加约束以使图像视图居中

  • -


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

    // 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];
      }
    
     }
    
    //放大和缩小后重新居中图像
    -(无效)centerScrollViewContents
    {
    CGSize boundsSize=self.bounds.size;
    CGRect contentsFrame=self.imageView.frame;
    if(contentsFrame.size.width1.0f)
    {
    [self-centerScrollViewContents];
    }
    其他的
    {
    self.bouncesZoom=否;
    //用于通过挤压缩小
    [self-centerScrollViewContents];
    }
    }
    

    这样,可以在放大和缩小图像后重新定位图像的坐标,而无需自动布局。如果有帮助,请告诉我。谢谢:)

    检查这个答案会对你有所帮助。那么,你是如何解决这个问题的?我正在努力用Autolayout实现相同的功能。@BlackFlam3使用了框架。
    // 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];
      }
    
     }