【问题标题】:UIScrollView zoom out issue (IOS 8 GM)UIScrollView 缩小问题(IOS 8 GM)
【发布时间】:2014-11-09 06:21:51
【问题描述】:

很长一段时间以来,我一直在努力解决我的UIScrollView 的问题。 基本上,它是一个简单的可缩放UIScrollView,显示UIImageView

当图片放大到最大时,我松开捏手势,动画很奇怪,不能平滑地放大到最小缩放比例。

它实际上可以在 Apple 的示例中重现:PhotoScroller 将图像缩小到最大,您会看到问题。

我发现它是对 iOS 8 中的 layoutSubviews 的额外调用(iOS 7 完美运行)。

有没有人遇到过这个问题,如果有,找到解决办法了吗?

【问题讨论】:

    标签: ios objective-c uiscrollview zooming ios8


    【解决方案1】:

    @Jonah 的修复为我解决了类似的问题,但重要的是不要直接调用 layoutSubviews。

    您可以使用以下代码实现类似且更安全的效果:

    - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
        [self setNeedsLayout]; // triggers a layout update during the next update cycle
        [self layoutIfNeeded]; // lays out the subviews immediately
    }
    

    有关更多信息,请参阅 Apple 的 UIView 文档: https://developer.apple.com/library/IOs/documentation/UIKit/Reference/UIView_Class/index.html#//apple_ref/occ/instm/UIView/layoutSubviews

    【讨论】:

      【解决方案2】:

      我可以通过调用 [self. layoutSubviews] 在我的 scrollViewDidZoom 方法中。这有点像黑客,但它似乎解决了我的问题。 这可能会有所帮助:

      - (void)scrollViewDidZoom:(UIScrollView *)scrollView{
          [self layoutSubviews];
      }
      

      覆盖 layoutSubviews 以使内容居中

      - (void)layoutSubviews
      {
      [super layoutSubviews];
      
      // center the image as it becomes smaller than the size of the screen
      CGSize boundsSize = super.bounds.size;
      CGRect frameToCenter = imageView.frame;
      
      // center horizontally
      if (frameToCenter.size.width < boundsSize.width){
          frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
      }
      else {
          frameToCenter.origin.x = 0;
      }
      // center vertically
      if (frameToCenter.size.height < boundsSize.height){
          frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
      }
      else {
          frameToCenter.origin.y = 0;
      }
      imageView.frame = frameToCenter;
      }
      

      【讨论】:

      • 很好,我自己也有同样的问题。但是,您不应该直接调用 layoutSubviews。文档说:“你不应该直接调用这个方法。如果你想强制更新布局,调用 setNeedsLayout 方法,而不是在下一次绘图更新之前这样做。如果你想立即更新视图的布局,调用layoutIfNeeded 方法”。我将居中代码添加到一个单独的方法中,并在 scrollViewDidZoom 方法中调用它
      猜你喜欢
      • 1970-01-01
      • 2015-12-19
      • 2020-05-06
      • 1970-01-01
      • 2011-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多