簡體   English   中英

是否可以在UIScrollView的內部放大和縮小UIImageView,但在“自動布局”中始終居中?

[英]Is it possible to have a UIImageView inside of a UIScrollView zoom in and out, but stick to center with Auto Layout?

長話短說,我正在嘗試構建類似於Photos.app的功能。

我在Storyboard中設置了帶有UIImageView的UIScrollView。 縮放有效,但我無法將其居中。 在所有基於框架的滾動視圖實現中,將其居中如下所示,效果很好:

- (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

我如何使用自動版式進行居中縮放?

使用Autolayout時,對setFrames的調用未生效,這就是為什么imageView不在scrollView中居中的原因。

話雖如此,為了達到中心效果,您可以在以下選項之間進行選擇:

  1. 最簡單的方法是設置translatesAutoresizingMaskIntoConstraintsYES在您的ImageView ViewDidLoad ,這將轉化為呼叫setFrame:根據您的ImageView新的約束autoresizingMask 但是,您應該確保在情節提要中設置的約束滿足新的約束(在您的情況下,沒有約束)。

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

--

 [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]];

要使內容居中,可以從以下代碼片段中獲取參考:

// 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];
  }

 }

這樣,您可以在放大和縮小后無需自動布局就可以重新定位圖像的坐標。 請告訴我是否有幫助。 謝謝 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM